From e9c7ed5ceb9623057b446bb539b7e06dac148b64 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sat, 11 Mar 2017 13:50:38 -0500 Subject: [PATCH] ### Added - Added logging of the savings for each image optimization if `devMode` is on --- CHANGELOG.md | 4 ++++ README.md | 7 +++++++ composer.json | 2 +- src/ImageOptim.php | 28 ++++++++++++++++++++++++++++ src/translations/en/imageoptim.php | 3 +++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59991b6b..9b1912fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # ImageOptim Changelog +## 1.0.2 - 2017.03.11 +### Added +- Added logging of the savings for each image optimization if `devMode` is on + ## 1.0.1 - 2017.03.11 ### Added - Added `mikehaertl/php-shellcommand` as a dependency in `composer.json` diff --git a/README.md b/README.md index a8675a32..0fcaced7 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,13 @@ See each image optimization tool's documentation for details on the options they Once ImageOptim is set up and configured, there's nothing left to do. It just works. +If you have `devMode` on, ImageOptim will log stats for images that it optimizes, e.g.: + +``` +2017-03-11 10:45:26 [192.168.10.1][1][-][info][nystudio107\imageoptim\{closure}] zappa.png -> Original: 129.5K, Optimized: 100.8K -- Savings: 28.4% + +``` + ## ImageOptim Roadmap Some things to do, and ideas for potential features: diff --git a/composer.json b/composer.json index 77e51ac7..2846e5fc 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.0", + "version": "1.0.2", "keywords": [ "craft", "cms", diff --git a/src/ImageOptim.php b/src/ImageOptim.php index 8991ae1e..5b69e298 100644 --- a/src/ImageOptim.php +++ b/src/ImageOptim.php @@ -54,11 +54,33 @@ function (GenerateTransformEvent $event) { $event->transformIndex, $event->image ); + $originalFileSize = filesize($tempPath); // Optimize the image ImageOptim::$plugin->optimize->optimizeImage( $event->transformIndex, $tempPath ); + // Log the results of the image optimization + $optimizedFileSize = filesize($tempPath); + $index = $event->transformIndex; + Craft::info( + pathinfo($index->filename, PATHINFO_FILENAME) + .'.'.$index->detectedFormat + . ' -> ' + . Craft::t('imageoptim', 'Original') + . ': ' + . $this->humanFileSize($originalFileSize, 1) + . ', ' + . Craft::t('imageoptim', 'Optimized') + . ': ' + . $this->humanFileSize($optimizedFileSize, 1) + . ' -- ' + . Craft::t('imageoptim', 'Savings') + . ': ' + . number_format(abs((1 - ($originalFileSize / $optimizedFileSize )) * 100), 1) + . '%', + __METHOD__ + ); // Return the path to the optimized image to _createTransformForAsset() $event->tempPath = $tempPath; } @@ -70,4 +92,10 @@ function (GenerateTransformEvent $event) { // Protected Methods // ========================================================================= + protected function humanFileSize($bytes, $decimals = 2): string + { + $sz = 'BKMGTP'; + $factor = floor((strlen($bytes) - 1) / 3); + return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[intval($factor)]; + } } diff --git a/src/translations/en/imageoptim.php b/src/translations/en/imageoptim.php index b0442968..4808446c 100644 --- a/src/translations/en/imageoptim.php +++ b/src/translations/en/imageoptim.php @@ -16,4 +16,7 @@ return [ 'plugin loaded' => 'plugin loaded', 'does not exist' => 'does not exist', + 'Original' => 'Original', + 'Optimized' => 'Optimized', + 'Savings' => 'Savings', ];