This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bruno Meilick <[email protected]>
- Loading branch information
0 parents
commit cf0da69
Showing
32 changed files
with
1,734 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"imageoptim/imageoptim": "^1.3" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/vendor/autoload.php'; | ||
|
||
class KirbyImageOptim { | ||
|
||
public static function is_localhost() { | ||
$whitelist = array( '127.0.0.1', '::1' ); | ||
if( in_array( $_SERVER['REMOTE_ADDR'], $whitelist) ) | ||
return true; | ||
} | ||
|
||
public static function imageoptim( | ||
$file, | ||
$width = false, | ||
$height = false, | ||
$crop = 'fit', | ||
$dpr = 1, | ||
$quality = 'medium') { | ||
|
||
$url = $file->url(); | ||
|
||
if($width == false) { | ||
$width = $file->width(); | ||
} | ||
|
||
if($height == false) { | ||
$height = round($file->height() * $width / $file->width()); | ||
} | ||
|
||
$imageoptimAPIKey = trim(c::get('plugin.imageoptim.apikey','')); | ||
|
||
// If can do imageoptim... | ||
if(!KirbyImageOptim::is_localhost() && | ||
c::get('plugin.imageoptim', false) && | ||
strlen($imageoptimAPIKey) > 0) { | ||
|
||
$wxh = $width.'x'.$height; | ||
$hash = sha1( | ||
$file->name().'-'. | ||
$wxh.'-'. | ||
$dpr.'-'. | ||
$quality.'-'. | ||
$file->modified()). | ||
'.'.$file->extension(); | ||
|
||
$filepath = str_replace( | ||
$file->filename(), | ||
$hash, | ||
kirby()->roots()->thumbs().DS.$file->uri()); | ||
|
||
$urlOptim = str_replace( | ||
$file->filename(), | ||
$hash, | ||
kirby()->urls()->thumbs().'/'.$file->uri()); | ||
|
||
if(!f::exists($filepath)) { | ||
$api = new ImageOptim\API($imageoptimAPIKey); | ||
try{ | ||
$imageData = $api->imageFromURL($file->url()) | ||
->quality($quality) | ||
->dpr(intval($dpr)) | ||
->resize($width, $height, $crop) | ||
->getBytes(); | ||
|
||
f::write($filepath, $imageData); | ||
$url = $urlOptim; | ||
} catch (Exception $ex) { | ||
return $ex->getMessage(); | ||
} | ||
} else { | ||
$url = $urlOptim; | ||
} | ||
|
||
// ... use kirby thumb instead | ||
} else { | ||
|
||
if($file->orientation() == 'portrait') { | ||
$nw = round($file->width() * $height / $file->height()); | ||
$url = $file->resize($nw, $height); | ||
} else { | ||
$url = $file->resize($width); | ||
} | ||
$url = str_replace(['<img src="','" alt="">'],['',''], $url); | ||
} | ||
|
||
return $url; | ||
} | ||
} | ||
|
||
$kirby->set('file::method', 'imageoptim', | ||
function($file, $width = false, $height = false, $crop = 'fit', $dpr = 1) { | ||
return KirbyImageOptim::imageoptim($file, $width, $height, $crop, $dpr); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "kirby-imageoptim", | ||
"description": "Kirby CMS file method to optimize images using ImageOptim within your template code.", | ||
"version": "1.0.0", | ||
"type": "kirby-plugin", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Kirby Imageoptim | ||
|
||
![GitHub release](https://img.shields.io/github/release/bnomei/kirby-imageoptim.svg?maxAge=1800) ![License](https://img.shields.io/github/license/mashape/apistatus.svg) ![Kirby Version](https://img.shields.io/badge/Kirby-2.3%2B-red.svg) | ||
|
||
Kirby CMS file method to optimize images using [ImageOptim PHP API](https://github.com/ImageOptim/php-imageoptim-api) within your template code. Optimized image is refreshed if file is changed or calling code requests different parameters. It is saved to the `/thumbs` folder (`kirby()->roots()->thumbs()`). | ||
|
||
Note: ImageOptim API will only be called on webserver. On localhost the kirby thumbs api will be used to avoid the timeconsuming [upload api call](https://github.com/ImageOptim/php-imageoptim-api#imagefrompathfilepath--local-source-image). | ||
|
||
## Requirements | ||
|
||
- [**Kirby**](https://getkirby.com/) 2.3+ | ||
- [ImageOptim API key](https://imageoptim.com/api/register) (trial available). This plugin uses v1.3.1. | ||
- `allow_url_fopen` PHP setting must be enabled for the API to work. Check with `ini_get('allow_url_fopen')`. Please be aware of the potential security risks caused by allow_url_fopen! | ||
|
||
## Installation | ||
|
||
### [Kirby CLI](https://github.com/getkirby/cli) | ||
|
||
``` | ||
kirby plugin:install bnomei/kirby-imageoptim | ||
``` | ||
|
||
### Git Submodule | ||
|
||
``` | ||
$ git submodule add https://github.com/bnomei/kirby-imageoptim.git site/plugins/kirby-imageoptim | ||
``` | ||
|
||
### Copy and Paste | ||
|
||
1. [Download](https://github.com/bnomei/kirby-imageoptim/archive/master.zip) the contents of this repository as ZIP-file. | ||
2. Rename the extracted folder to `kirby-imageoptim` and copy it into the `site/plugins/` directory in your Kirby project. | ||
|
||
## Usage | ||
|
||
In your `site/config.php` activate the plugin and set the [ImageOptim API key](https://imageoptim.com/api/register). | ||
|
||
``` | ||
c::set('plugin.imageoptim', true); // default is false | ||
c::set('plugin.imageoptim.apikey', 'YOUR_API_KEY_HERE'); | ||
``` | ||
|
||
The plugin adds a `$myFile->imageoptim()` function to [$file objects](https://getkirby.com/docs/cheatsheet#file). | ||
|
||
``` | ||
<?php | ||
// get any image/file object | ||
$myFile = $page->file('image.jpg'); | ||
// get url (on your webserver) for optimized thumb | ||
$url = $myFile->imageoptim(); | ||
// echo the url as image | ||
// https://getkirby.com/docs/toolkit/api#brick | ||
$img = brick('img') | ||
->attr('src', $url) | ||
->attr('alt', $myFile->filename()); | ||
echo $img; | ||
?> | ||
``` | ||
|
||
Changing width, height and/or fitting is also supported. Modifying dpr and quality setting as well. | ||
|
||
``` | ||
<?php | ||
// fit to 400px width | ||
$url = $myFile->imageoptim(400); | ||
// fit to 400px width and 300px height | ||
$url = $myFile->imageoptim(400, 300); | ||
// crop to 800x600px dimension | ||
$url = $myFile->imageoptim(800, 600, 'crop'); | ||
// fit to 400px width and 300px height at 2x dpr | ||
$url = $myFile->imageoptim(400, 300, 'fit', 2); | ||
// fit to 400px width and 300px height at 2x dpr and 'high' quality | ||
$url = $myFile->imageoptim(400, 300, 'fit', 2, 'high'); | ||
?> | ||
``` | ||
|
||
## Disclaimer | ||
|
||
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/bnomei/kirby-imageoptim/issues/new). | ||
|
||
## License | ||
|
||
[MIT](https://opensource.org/licenses/MIT) | ||
|
||
It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// autoload.php @generated by Composer | ||
|
||
require_once __DIR__ . '/composer' . '/autoload_real.php'; | ||
|
||
return ComposerAutoloaderInitdb60f64c7c86327cd38317daa842af05::getLoader(); |
Oops, something went wrong.