From 5ab8d83334e9e34d948578fd8611272e96564709 Mon Sep 17 00:00:00 2001 From: Sebastian Helzle Date: Thu, 19 Jan 2017 22:54:12 +0100 Subject: [PATCH 1/5] !!! TASK: Make libraries configurable This change makes it possible to use alternative libraries for image manipulation which might lead to better results based on the context. This change is breaking as the configuration is now based on the media type and not the file extension anymore. Resolves: #15 --- Classes/Aspects/ThumbnailAspect.php | 78 +++++++++++------------------ Configuration/Settings.yaml | 43 +++++++++++----- README.md | 23 +++++++++ 3 files changed, 83 insertions(+), 61 deletions(-) diff --git a/Classes/Aspects/ThumbnailAspect.php b/Classes/Aspects/ThumbnailAspect.php index a37d241..e55b1dd 100644 --- a/Classes/Aspects/ThumbnailAspect.php +++ b/Classes/Aspects/ThumbnailAspect.php @@ -1,6 +1,8 @@ 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 = []; diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index e1449d8..846f2b8 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -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 \ No newline at end of file + 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 diff --git a/README.md b/README.md index 8a3e441..ff07a94 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,29 @@ Usage of global available binaries can be configured instead or for specific for 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 + progressive: true + quality: 80 + library: 'jpegoptim' + binaryPath: 'jpegoptim-bin/vendor/jpegoptim' + arguments: "'--strip-all --max=' + quality + ' ' + (progressive ? '--all-progressive ' : '') + '-o ' + file" + +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 ----- From d41df11353085d622b8e05b4017a0a89b439147e Mon Sep 17 00:00:00 2001 From: Aske Ertmann Date: Fri, 20 Jan 2017 13:07:50 -0500 Subject: [PATCH 2/5] TASK: Adjust alternative library documentation example --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ff07a94..5f90e0b 100644 --- a/README.md +++ b/README.md @@ -68,15 +68,16 @@ Add the following to your `Settings` to use `jpegoptim` instead of `jpegtran`: formats: 'image/jpeg': enabled: true - progressive: true - quality: 80 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 ----- From a2949ddfdda35d318a19dd574fb2f3f7ee62d2e6 Mon Sep 17 00:00:00 2001 From: Aske Ertmann Date: Fri, 20 Jan 2017 13:17:03 -0500 Subject: [PATCH 3/5] TASK: Adjust Readme to 3.0 version and customizable libraries --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5f90e0b..94a5fcc 100644 --- a/README.md +++ b/README.md @@ -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`: ``` @@ -48,7 +48,7 @@ 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. From 12a1c3cb7ef30880ccdffbdfc677203310002c96 Mon Sep 17 00:00:00 2001 From: Aske Ertmann Date: Fri, 20 Jan 2017 14:20:29 -0500 Subject: [PATCH 4/5] TASK: Fix custom library example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 94a5fcc..0488d8c 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Add the following to your `Settings` to use `jpegoptim` instead of `jpegtran`: enabled: true library: 'jpegoptim' binaryPath: 'jpegoptim-bin/vendor/jpegoptim' - arguments: "'--strip-all --max=' + quality + ' ' + (progressive ? '--all-progressive ' : '') + '-o ' + file" + 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) From 7db745267fce918a3e8233386b1b6ec71cea68c0 Mon Sep 17 00:00:00 2001 From: Aske Ertmann Date: Fri, 20 Jan 2017 14:21:39 -0500 Subject: [PATCH 5/5] TASK: Remove trailing whitespace from settings configuration --- Configuration/Settings.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 846f2b8..b340c69 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -20,7 +20,7 @@ MOC: arguments: "${'-o' + optimizationLevel + ' -strip all -out ' + file + ' ' + file}" parameters: optimizationLevel: 2 # optimization level (0-7) - + 'image/gif': enabled: true useGlobalBinary: false # use globally installed binary for the gif library instead @@ -29,11 +29,11 @@ MOC: arguments: "${'--batch -O' + optimizationLevel + ' ' + file}" parameters: optimizationLevel: 2 # optimization level (1-3) - + 'image/svg+xml': enabled: true useGlobalBinary: false # use globally installed binary for the svg library instead - library: 'svgo' + library: 'svgo' binaryPath: 'svgo/bin/svgo' arguments: "${(pretty ? '--pretty ' : '') + file}" parameters: