Skip to content
This repository has been archived by the owner on Aug 21, 2019. It is now read-only.

Commit

Permalink
added option for filename and media object
Browse files Browse the repository at this point in the history
#1
Signed-off-by: Bruno Meilick <[email protected]>
  • Loading branch information
bnomei committed Jun 19, 2018
1 parent 2a18d54 commit 9174405
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 96 deletions.
3 changes: 1 addition & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

179 changes: 98 additions & 81 deletions kirby-imageoptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,111 @@

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 = null,
$height = null,
$crop = 'fit',
$dpr = 1,
$quality = 'medium') {

$url = $file->url();

if(!$width) {
$width = $file->width();
}

if(!$height) {
$height = round($file->height() * $width / $file->width());
class KirbyImageOptim
{
public static function is_localhost()
{
$whitelist = array( '127.0.0.1', '::1' );
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
return true;
}
}

$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());
public static function imageoptim(
$file,
$width = null,
$height = null,
$crop = 'fit',
$dpr = 1,
$quality = 'medium',
$media = null
) {
$url = $file->url();

if (!$width) {
$width = $file->width();
}

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();
if (!$height) {
$height = round($file->height() * $width / $file->width());
}
} else {
$url = $urlOptim;
}

// ... use kirby thumb instead
} else {
if (!$media) {
$media = c::get('plugin.imageoptim.media', false);
}

if($file->orientation() == 'portrait') {
$nw = round($file->width() * $height / $file->height());
$url = $file->thumb($nw, $height);
} else {
$url = $file->thumb($width);
}
$url = str_replace(['<img src="','" alt="">'],['',''], $url);
$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();

if (c::get('plugin.imageoptim.filename', false)) {
$hash = $file->name().'-'.$hash;
}

$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;
}

if ($media) {
$url = new Media($filepath, $url);
}
// ... use kirby thumb instead
} else {
if ($file->orientation() == 'portrait') {
$nw = round($file->width() * $height / $file->height());
$url = thumb($file, ['width' => $nw, 'height' => $height], $media);
} else {
$url = thumb($file, ['width' => $width, 'height' => $height], $media);
}
if (!$media) {
$url = str_replace(['<img src="','" alt="">'], ['',''], $url);
}
}
return $url;
}

return $url;
}
}

$kirby->set('file::method', 'imageoptim',
function($file, $width = null, $height = null, $crop = 'fit', $dpr = 1) {
return KirbyImageOptim::imageoptim($file, $width, $height, $crop, $dpr);
});
$kirby->set(
'file::method',
'imageoptim',
function ($file, $width = null, $height = null, $crop = 'fit', $dpr = 1, $quality = 'medium', $media = null) {
return KirbyImageOptim::imageoptim($file, $width, $height, $crop, $dpr, $quality, $media);
}
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kirby-imageoptim",
"description": "Kirby CMS file method to optimize images using ImageOptim within your template code.",
"version": "1.0.1",
"version": "1.1.0",
"type": "kirby-plugin",
"license": "MIT"
}
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ $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');

// fit to 400px width and 300px height at 2x dpr and 'high' quality, return Media-Object instead of url
$media = $myFile->imageoptim(400, 300, 'fit', 2, 'high', true);
```

## Other default settings

In your `site/config.php`...

```php
c::get('plugin.imageoptim.media', false); // default false
// since v1.1.0
// if false will return url else Media-Object

c::get('plugin.imageoptim.filename', false); // default false
// since v1.1.0
// if false filename will be {hash} only else {filename-hash}
```

## Disclaimer
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

// autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';
require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitdb60f64c7c86327cd38317daa842af05::getLoader();
46 changes: 38 additions & 8 deletions vendor/composer/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ClassLoader
private $classMap = array();
private $classMapAuthoritative = false;
private $missingClasses = array();
private $apcuPrefix;

public function getPrefixes()
{
Expand Down Expand Up @@ -271,6 +272,26 @@ public function isClassMapAuthoritative()
return $this->classMapAuthoritative;
}

/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
}

/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}

/**
* Registers this instance as an autoloader.
*
Expand Down Expand Up @@ -313,18 +334,19 @@ public function loadClass($class)
*/
public function findFile($class)
{
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
if ('\\' == $class[0]) {
$class = substr($class, 1);
}

// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}

$file = $this->findFileWithExtension($class, '.php');

Expand All @@ -333,6 +355,10 @@ public function findFile($class)
$file = $this->findFileWithExtension($class, '.hh');
}

if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}

if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
Expand All @@ -348,9 +374,13 @@ private function findFileWithExtension($class, $ext)

$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath.'\\';
if (isset($this->prefixDirsPsr4[$search])) {
foreach ($this->prefixDirsPsr4[$search] as $dir) {
$length = $this->prefixLengthsPsr4[$first][$search];
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/composer/LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Copyright (c) 2016 Nils Adermann, Jordi Boggiano
Copyright (c) Nils Adermann, Jordi Boggiano

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function getLoader()
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInitdb60f64c7c86327cd38317daa842af05', 'loadClassLoader'));

$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION');
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';

Expand Down
2 changes: 1 addition & 1 deletion vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require-dev": {
"phpunit/phpunit": "^5.3"
},
"time": "2017-01-09 23:58:20",
"time": "2017-01-09T23:58:20+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
Expand Down
Empty file modified vendor/imageoptim/imageoptim/examples/optimize_directory.php
100755 → 100644
Empty file.

0 comments on commit 9174405

Please sign in to comment.