Skip to content

Commit

Permalink
### Added
Browse files Browse the repository at this point in the history
- Added logging of the savings for each image optimization if `devMode` is on
  • Loading branch information
khalwat committed Mar 11, 2017
1 parent d87ad4b commit e9c7ed5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions src/ImageOptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)];
}
}
3 changes: 3 additions & 0 deletions src/translations/en/imageoptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
return [
'plugin loaded' => 'plugin loaded',
'does not exist' => 'does not exist',
'Original' => 'Original',
'Optimized' => 'Optimized',
'Savings' => 'Savings',
];

0 comments on commit e9c7ed5

Please sign in to comment.