diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..76f8b642 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# COMPOSER +/vendor + +# BUILD FILES +/bower_components/* +/node_modules/* +/build/* + +# MISC FILES +.cache +.DS_Store +.idea +.project +.settings +*.esproj +*.sublime-workspace +*.sublime-project +*.tmproj +*.tmproject diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..eb9e0861 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# ImageOptim Changelog + +## 1.0.0 - 2017.03.11 +### Added +- Initial release diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..4be45de1 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2017 nystudio107 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 00000000..f85fdc65 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# ImageOptim plugin for Craft CMS 3.x + +Automatically optimize images after they've been transformed + +## Installation + +To install ImageOptim, follow these steps: + +1. Install with Composer via `composer require nystudio107/craft3-imageoptim` +2. Install plugin in the Craft Control Panel under Settings > Plugins + +ImageOptim works on Craft 3.x. + +ImageOptim won't do anything on its own; you'll need to also install the image optimization tools of your choice. Here's how to install a few on Ubuntu 16.04: + +* **jpegoptim** - `sudo apt-get install jpegoptim` +* **mozjpeg** - [Installing mozjpeg on Ubuntu 16.04 (Forge)](https://nystudio107.com/blog/installing-mozjpeg-on-ubuntu-16-04-forge) +* **optipng** - `sudo apt-get install optipng` +* **svgo** - `sudo npm install -g svgo` + +## ImageOptim Overview + +ImageOptim allows you to optimize the images created by Craft 3's Image Transforms by automatically running a variety of image optimization tools on them. As configured by default, all of these are _lossless_ image optimizations that remove metadata and otherwise optimize the images without change their appearance in any way. + +It's important to create optimized images for frontend delivery, especially for mobile devices. If you want to learn more about it, read the [Creating Optimized Images in Craft CMS](https://nystudio107.com/blog/creating-optimized-images-in-craft-cms) article. + +Once ImageOptim is installed, optimized versions of image transforms are created with you having to do anything. This makes it great for client-proofing websites. + +## Configuring ImageOptim + +The only configuration for ImageOptim is in the `config.php` file, which is a multi-environment friendly way to store the default settings. Don't edit this file, instead copy it to `craft/config` as `imageoptim.php` and make your changes there. + +The `activeProcessors` array lets you specify which of the image optimization tools to use for which file types. + +The `imageProcessors` array specifies the path and options for each of the image optimization tools. + +## Using ImageOptim + +Once ImageOptim is set up and configured, there's nothing left to do. It just works. + +## ImageOptim Roadmap + +Some things to do, and ideas for potential features: + +* Add support for addition image optimization tools + +Brought to you by [nystudio107](https://nystudio107.com) diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..ef97272f --- /dev/null +++ b/composer.json @@ -0,0 +1,43 @@ +{ + "name": "nystudio107/craft3-imageoptim", + "description": "Automatically optimize images after they've been transformed", + "type": "craft-plugin", + "version": "1.0.0", + "keywords": [ + "craft", + "cms", + "craftcms", + "craft-plugin", + "imageoptim" + ], + "support": { + "docs": "https://github.com/nystudio107/craft3-imageoptim/blob/master/README.md", + "issues": "https://github.com/nystudio107/craft3-imageoptim/issues" + }, + "license": "MIT", + "authors": [ + { + "name": "nystudio107", + "homepage": "https://nystudio107.com" + } + ], + "require": { + }, + "autoload": { + "psr-4": { + "nystudio107\\imageoptim\\": "src/" + } + }, + "extra": { + "name": "ImageOptim", + "handle": "imageOptim", + "schemaVersion": "1.0.0", + "hasSettings": false, + "hasCpSection": false, + "changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft3-imageoptim/master/CHANGELOG.md", + "components": { + "optimize": "nystudio107\\imageoptim\\services\\Optimize" + }, + "class": "nystudio107\\imageoptim\\ImageOptim" + } +} diff --git a/resources/img/plugin-logo.png b/resources/img/plugin-logo.png new file mode 100644 index 00000000..1b3542c0 Binary files /dev/null and b/resources/img/plugin-logo.png differ diff --git a/src/ImageOptim.php b/src/ImageOptim.php new file mode 100644 index 00000000..8991ae1e --- /dev/null +++ b/src/ImageOptim.php @@ -0,0 +1,73 @@ +optimize->saveTransformToTempFile( + $event->transformIndex, + $event->image + ); + // Optimize the image + ImageOptim::$plugin->optimize->optimizeImage( + $event->transformIndex, + $tempPath + ); + // Return the path to the optimized image to _createTransformForAsset() + $event->tempPath = $tempPath; + } + ); + + Craft::info('ImageOptim ' . Craft::t('imageoptim', 'plugin loaded'), __METHOD__); + } + + // Protected Methods + // ========================================================================= + +} diff --git a/src/config.php b/src/config.php new file mode 100644 index 00000000..b57b8573 --- /dev/null +++ b/src/config.php @@ -0,0 +1,76 @@ + [ + "jpg" => [ + "jpegoptim", + ], + "png" => [ + "optipng", + ], + "svg" => [ + "svgo", + ], + ], + + // Preset image processors + "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" => "", + ], + ], + +]; diff --git a/src/icon.svg b/src/icon.svg new file mode 100644 index 00000000..c0b50979 --- /dev/null +++ b/src/icon.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/services/Optimize.php b/src/services/Optimize.php new file mode 100644 index 00000000..7445e0ea --- /dev/null +++ b/src/services/Optimize.php @@ -0,0 +1,122 @@ +filename, PATHINFO_FILENAME), true).'.'.$index->detectedFormat; + $tempPath = Craft::$app->getPath()->getTempPath().DIRECTORY_SEPARATOR.$tempFilename; + $image->saveAs($tempPath); + Craft::info('Transformed image saved to: ' . $tempPath, __METHOD__); + + return $tempPath; + } + + /** + * Run any image post-processing/optimization on the image file + * + * @param AssetTransformIndex $index + * @param string $tempPath + */ + public function optimizeImage(AssetTransformIndex $index, string $tempPath) + { + // Get the active processors for the transform format + $activeProcessors = Craft::$app->config->get("activeProcessors", "imageoptim"); + $fileFormat = $index->detectedFormat; + if (!empty($activeProcessors[$fileFormat])) { + // Iterate through all of the processors for this format + $imageProcessors = Craft::$app->config->get("imageProcessors", "imageoptim"); + foreach ($activeProcessors[$fileFormat] as $processor) { + if (!empty($imageProcessors[$processor])) { + // Make sure the command exists + $thisProcessor = $imageProcessors[$processor]; + if (file_exists($thisProcessor['commandPath'])) { + // Build the command to execute + $cmd = + $thisProcessor['commandPath'] + ." " + .$thisProcessor['commandOptions'] + ." " + .escapeshellarg($tempPath); + // Execute the command + $shellOutput = $this->_executeShellCommand($cmd); + Craft::info($cmd."\n".$shellOutput, __METHOD__); + } else { + Craft::error( + $thisProcessor['commandPath'] + . " " + . Craft::t("imageoptim", "does not exist"), + __METHOD__ + ); + } + } + } + } + } + + // Private Methods + // ========================================================================= + + /** + * Execute a shell command + * + * @param string $command + * + * @return string + */ + private function _executeShellCommand(string $command): string + { + // Create the shell command + $shellCommand = new ShellCommand(); + $shellCommand->setCommand($command); + + // If we don't have proc_open, maybe we've got exec + if (!function_exists('proc_open') && function_exists('exec')) { + $shellCommand->useExec = true; + } + + // Return the result of the command's output or error + if ($shellCommand->execute()) { + $result = $shellCommand->getOutput(); + } else { + $result = $shellCommand->getError(); + } + + return $result; + } +} diff --git a/src/translations/en/imageoptim.php b/src/translations/en/imageoptim.php new file mode 100644 index 00000000..b0442968 --- /dev/null +++ b/src/translations/en/imageoptim.php @@ -0,0 +1,19 @@ + 'plugin loaded', + 'does not exist' => 'does not exist', +];