From 14cd5bbb234d7220880302df04a4da3a27523f52 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sat, 25 Mar 2017 01:27:52 -0400 Subject: [PATCH] ### Changed * `hasSettings` -> `hasCpSettings` for Craft 3 beta 8 compatibility * Added Craft 3 beta 8 compatible settings * Modified config service calls for Craft 3 beta 8 --- CHANGELOG.md | 6 ++ composer.json | 4 +- src/ImageOptim.php | 9 ++- src/config.php | 7 +- src/models/Settings.php | 104 +++++++++++++++++++++++++++++ src/services/Optimize.php | 5 +- src/translations/en/imageoptim.php | 2 +- 7 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 src/models/Settings.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f1a1de3..08138ecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # ImageOptim Changelog +## 1.0.5 - 2017.03.24 +### Changed +* `hasSettings` -> `hasCpSettings` for Craft 3 beta 8 compatibility +* Added Craft 3 beta 8 compatible settings +* Modified config service calls for Craft 3 beta 8 + ## 1.0.4 - 2017.03.12 ### Added - Added code inspection typehinting for the plugin & services diff --git a/composer.json b/composer.json index 07bc1d86..aa983cfd 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft3-imageoptim", "description": "Automatically optimize images after they've been transformed", "type": "craft-plugin", - "version": "1.0.4", + "version": "1.0.5", "keywords": [ "craft", "cms", @@ -34,7 +34,7 @@ "name": "ImageOptim", "handle": "imageOptim", "schemaVersion": "1.0.0", - "hasSettings": false, + "hasCpSettings": false, "hasCpSection": false, "changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft3-imageoptim/master/CHANGELOG.md", "components": { diff --git a/src/ImageOptim.php b/src/ImageOptim.php index baa89e7f..694e6344 100644 --- a/src/ImageOptim.php +++ b/src/ImageOptim.php @@ -61,6 +61,13 @@ function (GenerateTransformEvent $event) { } ); - Craft::info('ImageOptim ' . Craft::t('imageoptim', 'plugin loaded'), __METHOD__); + Craft::info( + Craft::t( + 'imageoptim', + '{name} plugin loaded', + ['name' => $this->name] + ), + __METHOD__ + ); } } diff --git a/src/config.php b/src/config.php index 3b0b252d..782215e3 100644 --- a/src/config.php +++ b/src/config.php @@ -11,12 +11,11 @@ /** * ImageOptim config.php * - * Completely optional configuration settings for ImageOptim if you want to - * customize some of its more esoteric behavior, or just want specific control - * over things. + * This file exists only as a template for the ImageOptim settings. + * It does nothing on its own. * * Don't edit this file, instead copy it to 'craft/config' as 'imageoptim.php' - * and make your changes there. + * and make your changes there to override default settings. * * Once copied to 'craft/config', this file will be multi-environment aware as * well, so you can have different settings groups for each environment, just as diff --git a/src/models/Settings.php b/src/models/Settings.php new file mode 100644 index 00000000..a904e129 --- /dev/null +++ b/src/models/Settings.php @@ -0,0 +1,104 @@ + [ + 'jpegoptim', + ], + 'png' => [ + 'optipng', + ], + 'svg' => [ + 'svgo', + ], + 'gif' => [ + 'gifsicle', + ], + ]; + + /** + * Preset image processors + * + * @var array + */ + public $imageProcessors = [ + // jpeg optimizers + 'jpegoptim' => [ + 'commandPath' => '/usr/bin/jpegoptim', + 'commandOptions' => '-s', + ], + 'mozjpeg' => [ + 'commandPath' => '/usr/bin/mozjpeg', + 'commandOptions' => '-optimize -copy none', + ], + 'jpegtran' => [ + 'commandPath' => '/usr/bin/jpegtran', + 'commandOptions' => '-optimize -copy none', + ], + // png optimizers + 'optipng' => [ + 'commandPath' => '/usr/bin/optipng', + 'commandOptions' => '-o7 -strip all', + ], + 'pngcrush' => [ + 'commandPath' => '/usr/bin/pngcrush', + 'commandOptions' => '-brute -ow', + ], + 'pngquant' => [ + 'commandPath' => '/usr/bin/pngquant', + 'commandOptions' => '--strip --skip-if-larger', + ], + // svg optimizers + 'svgo' => [ + 'commandPath' => '/usr/bin/svgo', + 'commandOptions' => '', + ], + // gif optimizers + 'gifsicle' => [ + 'commandPath' => '/usr/bin/gifsicle', + 'commandOptions' => '-O3 -k 256', + ], + ]; + + // Public Methods + // ========================================================================= + + /** + * @inheritdoc + */ + public function rules() + { + return [ + ['activeImageProcessors', 'required'], + ['imageProcessors', 'required'], + ]; + } +} diff --git a/src/services/Optimize.php b/src/services/Optimize.php index 6fe66043..9d90e8f6 100644 --- a/src/services/Optimize.php +++ b/src/services/Optimize.php @@ -102,12 +102,13 @@ public function saveTransformToTempFile(AssetTransformIndex $index, Image $image */ public function optimizeImage(AssetTransformIndex $index, string $tempPath) { + $settings = ImageOptim::$plugin->getSettings(); // Get the active processors for the transform format - $activeImageProcessors = Craft::$app->config->get('activeImageProcessors', 'imageoptim'); + $activeImageProcessors = $settings['activeImageProcessors']; $fileFormat = $index->detectedFormat; if (!empty($activeImageProcessors[$fileFormat])) { // Iterate through all of the processors for this format - $imageProcessors = Craft::$app->config->get('imageProcessors', 'imageoptim'); + $imageProcessors = $settings['imageProcessors']; foreach ($activeImageProcessors[$fileFormat] as $processor) { if (!empty($imageProcessors[$processor])) { // Make sure the command exists diff --git a/src/translations/en/imageoptim.php b/src/translations/en/imageoptim.php index 4808446c..395b8225 100644 --- a/src/translations/en/imageoptim.php +++ b/src/translations/en/imageoptim.php @@ -14,7 +14,7 @@ * @since 1.0.0 */ return [ - 'plugin loaded' => 'plugin loaded', + 'ImageOptim plugin loaded' => 'ImageOptim plugin loaded', 'does not exist' => 'does not exist', 'Original' => 'Original', 'Optimized' => 'Optimized',