Skip to content

Commit

Permalink
Merge pull request #17 from Sebobo/task-configurable-libraries
Browse files Browse the repository at this point in the history
!!! TASK: Make libraries configurable
  • Loading branch information
aertmann committed Jan 20, 2017
2 parents 503eaef + 7db7452 commit 5dc9f77
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 68 deletions.
78 changes: 29 additions & 49 deletions Classes/Aspects/ThumbnailAspect.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace MOC\ImageOptimizer\Aspects;

use Neos\Eel\CompilingEvaluator;
use Neos\Eel\Utility;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Log\SystemLoggerInterface;
Expand Down Expand Up @@ -31,6 +33,12 @@ class ThumbnailAspect
*/
protected $resourceManager;

/**
* @Flow\Inject
* @var CompilingEvaluator
*/
protected $eelEvaluator;

/**
* @var array
*/
Expand Down Expand Up @@ -76,56 +84,28 @@ public function optimizeThumbnail(JoinPointInterface $joinPoint)
$binaryRootPath = 'Private/Library/node_modules/';
$file = escapeshellarg($pathAndFilename);
$imageType = $thumbnailResource->getMediaType();
switch ($imageType) {
case 'image/jpeg':
if ($this->settings['formats']['jpg']['enabled'] === false) {
return;
}
$library = 'jpegtran';
$binaryPath = sprintf('%1$s-bin/vendor/%s', $library);
$arguments = sprintf('-copy none -optimize %s -outfile %s %s', $this->settings['formats']['jpg']['progressive'] === true ? '-progressive' : '', $file, $file);
if ($this->settings['formats']['jpg']['useGlobalBinary'] === true) {
$useGlobalBinary = true;
}
break;
case 'image/png':
if ($this->settings['formats']['png']['enabled'] === false) {
return;
}
$library = 'optipng';
$binaryPath = sprintf('%1$s-bin/vendor/%s', $library);
$arguments = sprintf('-o%u -strip all -out %s %s', $this->settings['formats']['png']['optimizationLevel'], $file, $file);
if ($this->settings['formats']['png']['useGlobalBinary'] === true) {
$useGlobalBinary = true;
}
break;
case 'image/gif':
if ($this->settings['formats']['gif']['enabled'] === false) {
return;
}
$library = 'gifsicle';
$binaryPath = sprintf('%1$s/vendor/%1$s', $library);
$arguments = sprintf('--batch -O%u %s ', $this->settings['formats']['gif']['optimizationLevel'], $file);
if ($this->settings['formats']['gif']['useGlobalBinary'] === true) {
$useGlobalBinary = true;
}
break;
case 'image/svg+xml':
if ($this->settings['formats']['svg']['enabled'] === false) {
return;
}
$library = 'svgo';
$binaryPath = sprintf('%1$s/bin/%1$s', $library);
$arguments = sprintf('%s %s', $this->settings['formats']['svg']['pretty'] === true ? '--pretty' : '', $file);
if ($this->settings['formats']['svg']['useGlobalBinary'] === true) {
$useGlobalBinary = true;
}
break;
default:
$this->systemLogger->log(sprintf('Unsupported type "%s" skipped in optimizeThumbnail', $imageType), LOG_INFO);
return;
break;

if (!array_key_exists($imageType, $this->settings['formats'])) {
$this->systemLogger->log(sprintf('Unsupported type "%s" skipped in optimizeThumbnail', $imageType), LOG_INFO);
return;
}

$librarySettings = $this->settings['formats'][$imageType];

if ($librarySettings['enabled'] === false) {
return;
}

if ($librarySettings['useGlobalBinary'] === true) {
$useGlobalBinary = true;
}

$library = $librarySettings['library'];
$binaryPath = $librarySettings['binaryPath'];
$eelExpression = $librarySettings['arguments'];
$parameters = array_merge($librarySettings['parameters'], ['file' => $file]);
$arguments = Utility::evaluateEelExpression($eelExpression, $this->eelEvaluator, $parameters);

$binaryPath = $useGlobalBinary === true ? $this->settings['globalBinaryPath'] . $library : $this->packageManager->getPackage('MOC.ImageOptimizer')->getResourcesPath() . $binaryRootPath . $binaryPath;
$cmd = escapeshellcmd($binaryPath) . ' ' . $arguments;
$output = [];
Expand Down
43 changes: 31 additions & 12 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@ MOC:
useGlobalBinary: false # use globally installed binaries for all formats instead
globalBinaryPath: ''
formats:
jpg:
'image/jpeg':
enabled: true
progressive: true # whether or not to server progressive jpgs
useGlobalBinary: false # use globally installed binary for jpegtran instead
png:
useGlobalBinary: false # use globally installed binary for the jpeg library instead
library: 'jpegtran'
binaryPath: 'jpegtran-bin/vendor/jpegtran'
arguments: "${'-copy none -optimize ' + (progressive ? '-progressive ' : '') + '-outfile ' + file + ' ' + file}"
parameters:
progressive: true # whether or not to serve progressive jpgs

'image/png':
enabled: true
optimizationLevel: 2 # optimization level (0-7)
useGlobalBinary: false # use globally installed binary for optipng instead
gif:
useGlobalBinary: false # use globally installed binary for the png library instead
library: 'optipng'
binaryPath: 'optipng-bin/vendor/optipng'
arguments: "${'-o' + optimizationLevel + ' -strip all -out ' + file + ' ' + file}"
parameters:
optimizationLevel: 2 # optimization level (0-7)

'image/gif':
enabled: true
optimizationLevel: 2 # optimization level (1-3)
useGlobalBinary: false # use globally installed binary for gifsicle instead
svg:
useGlobalBinary: false # use globally installed binary for the gif library instead
library: 'gifsicle'
binaryPath: 'gifsicle/vendor/gifsicle'
arguments: "${'--batch -O' + optimizationLevel + ' ' + file}"
parameters:
optimizationLevel: 2 # optimization level (1-3)

'image/svg+xml':
enabled: true
pretty: false # keep the output readable
useGlobalBinary: false # use globally installed binary for svgo instead
useGlobalBinary: false # use globally installed binary for the svg library instead
library: 'svgo'
binaryPath: 'svgo/bin/svgo'
arguments: "${(pretty ? '--pretty ' : '') + file}"
parameters:
pretty: false # keep the output readable
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ MOC.ImageOptimizer
Introduction
------------

Neos CMS / Flow framework package that optimizes generated thumbnail images (jpg, png, gif, svg) for web presentation.
Neos CMS / Flow framework package that optimizes generated thumbnail images (jpg, png, gif, svg and more) for web presentation.

Original files are never affected since copies are always created for thumbnails.

Non-blocking during rendering (asynchronous) optimization.

Using jpegtran, optipng, gifsicle and svgo for the optimizations.
Using jpegtran, optipng, gifsicle and svgo or alternative customizible ones for the optimizations.

Should work with Linux, FreeBSD, OSX, SunOS & Windows (only tested Linux & FreeBSD so far).

Compatible with Neos 1.x-2.x+ / Flow 2.x-3.x+
Compatible with Neos 1.x, 2.x, 3.x+ / Flow 2.x, 3.x, 4.x+

##### Only supports local file system (no CDN support yet)
##### Only supports local file system (no CDN support yet) (see #10)

Installation
------------

Requires npm (node.js) to work out of the box, although binaries can also be installed manually without it.

`composer require "moc/imageoptimizer" "~2.0"`
`composer require "moc/imageoptimizer" "~3.0"`

Ensure the image manipulation libraries `jpegtran` (JPG), `optipng` (PNG), `gifsicle` (GIF) and `svgo` (SVG) are installed globally. Libraries can be skipped if desired.
Ensure the image manipulation libraries `jpegtran` (JPG), `optipng` (PNG), `gifsicle` (GIF) and `svgo` (SVG) are installed globally. Libraries can be skipped if desired, just make sure to disable those mimetypes.

Alternatively install them using `npm`:
```
Expand All @@ -48,12 +48,36 @@ Using the `Settings` configuration, multiple options can be adjusted.

Optimization can be disabled for specific file formats.

Additionally options for optimization level (png & gif), progressive (jpg), pretty (svg) can be adjusted.
Additionally options such as optimization level (png & gif), progressive (jpg), pretty (svg) can be adjusted depending on optimization library.

Usage of global available binaries can be configured instead or for specific formats.

Enable using the setting `MOC.ImageOptimizer.useGlobalBinary` and configure the path in `MOC.ImageOptimizer.globalBinaryPath`.

Use alternative libraries for optimization
------------------------------------------

You can replace the preconfigured libraries with alternative ones.

Example:

Add the following to your `Settings` to use `jpegoptim` instead of `jpegtran`:

MOC:
ImageOptimizer:
formats:
'image/jpeg':
enabled: true
library: 'jpegoptim'
binaryPath: 'jpegoptim-bin/vendor/jpegoptim'
arguments: "${'--strip-all --max=' + quality + ' ' + (progressive ? '--all-progressive ' : '') + '-o ' + file}"
parameters:
progressive: true # whether or not to serve progressive jpgs
quality: 80 # quality level (1-100)

When doing this you have to take care that you provide the necessary library yourself as it's not included
when doing the installation like described above.

Usage
-----

Expand Down

0 comments on commit 5dc9f77

Please sign in to comment.