From 7fa7178651ebd3238ce3c4ab3af34af18854d991 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 24 May 2018 18:48:03 -0400 Subject: [PATCH 1/4] Handle JPEGs coming in as both `jpg` & `jpeg` Signed-off-by: Andrew Welch --- src/services/Optimize.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/services/Optimize.php b/src/services/Optimize.php index 8e84d0a0..8ecec4ee 100644 --- a/src/services/Optimize.php +++ b/src/services/Optimize.php @@ -209,6 +209,10 @@ public function optimizeImage(AssetTransformIndex $index, string $tempPath) // Get the active processors for the transform format $activeImageProcessors = $settings->activeImageProcessors; $fileFormat = $index->detectedFormat; + // Special-case for 'jpeg' + if ($fileFormat === 'jpeg') { + $fileFormat = 'jpg'; + } if (!empty($activeImageProcessors[$fileFormat])) { // Iterate through all of the processors for this format $imageProcessors = $settings->imageProcessors; @@ -255,6 +259,10 @@ public function createImageVariants(AssetTransformIndex $index, Asset $asset, st // Get the active image variant creators $activeImageVariantCreators = $settings->activeImageVariantCreators; $fileFormat = $index->detectedFormat ?? $index->format; + // Special-case for 'jpeg' + if ($fileFormat === 'jpeg') { + $fileFormat = 'jpg'; + } if (!empty($activeImageVariantCreators[$fileFormat])) { // Iterate through all of the image variant creators for this format $imageVariantCreators = $settings->imageVariantCreators; From 5a41799046e00c1a863a57966bbd63c7c4bdd26a Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 24 May 2018 18:53:03 -0400 Subject: [PATCH 2/4] Remove vestigal empty() checks Signed-off-by: Andrew Welch --- src/services/Optimize.php | 122 ++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 63 deletions(-) diff --git a/src/services/Optimize.php b/src/services/Optimize.php index 8ecec4ee..8f288a7b 100644 --- a/src/services/Optimize.php +++ b/src/services/Optimize.php @@ -69,8 +69,8 @@ public function handleGetAssetUrlEvent(GetAssetUrlEvent $event) // If we're passed in null, make a dummy AssetTransform model if (empty($transform)) { $transform = new AssetTransform([ - 'height' => $asset->height, - 'width' => $asset->width, + 'height' => $asset->height, + 'width' => $asset->width, 'interlace' => 'line', ]); } @@ -216,11 +216,9 @@ public function optimizeImage(AssetTransformIndex $index, string $tempPath) if (!empty($activeImageProcessors[$fileFormat])) { // Iterate through all of the processors for this format $imageProcessors = $settings->imageProcessors; - if (!empty($activeImageProcessors[$fileFormat])) { - foreach ($activeImageProcessors[$fileFormat] as $processor) { - if (!empty($processor) && !empty($imageProcessors[$processor])) { - $this->executeImageProcessor($imageProcessors[$processor], $tempPath); - } + foreach ($activeImageProcessors[$fileFormat] as $processor) { + if (!empty($processor) && !empty($imageProcessors[$processor])) { + $this->executeImageProcessor($imageProcessors[$processor], $tempPath); } } } @@ -266,55 +264,53 @@ public function createImageVariants(AssetTransformIndex $index, Asset $asset, st if (!empty($activeImageVariantCreators[$fileFormat])) { // Iterate through all of the image variant creators for this format $imageVariantCreators = $settings->imageVariantCreators; - if (!empty($activeImageVariantCreators[$fileFormat])) { - foreach ($activeImageVariantCreators[$fileFormat] as $variantCreator) { - if (!empty($variantCreator) && !empty($imageVariantCreators[$variantCreator])) { - // Create the image variant in a temporary folder - $generalConfig = Craft::$app->getConfig()->getGeneral(); - $quality = $index->transform->quality ?: $generalConfig->defaultImageQuality; - $outputPath = $this->executeVariantCreator( - $imageVariantCreators[$variantCreator], - $tempPath, - $quality - ); + foreach ($activeImageVariantCreators[$fileFormat] as $variantCreator) { + if (!empty($variantCreator) && !empty($imageVariantCreators[$variantCreator])) { + // Create the image variant in a temporary folder + $generalConfig = Craft::$app->getConfig()->getGeneral(); + $quality = $index->transform->quality ?: $generalConfig->defaultImageQuality; + $outputPath = $this->executeVariantCreator( + $imageVariantCreators[$variantCreator], + $tempPath, + $quality + ); - if (!empty($outputPath)) { - // Get info on the original and the created variant - $originalFileSize = @filesize($tempPath); - $variantFileSize = @filesize($outputPath); - - Craft::info( - pathinfo($tempPath, PATHINFO_FILENAME) - .'.' - .pathinfo($tempPath, PATHINFO_EXTENSION) - .' -> ' - .pathinfo($outputPath, PATHINFO_FILENAME) - .'.' - .pathinfo($outputPath, PATHINFO_EXTENSION) - .' -> ' - .Craft::t('image-optimize', 'Original') - .': ' - .$this->humanFileSize($originalFileSize, 1) - .', ' - .Craft::t('image-optimize', 'Variant') - .': ' - .$this->humanFileSize($variantFileSize, 1) - .' -> ' - .Craft::t('image-optimize', 'Savings') - .': ' - .number_format(abs(100 - (($variantFileSize * 100) / $originalFileSize)), 1) - .'%', - __METHOD__ - ); + if (!empty($outputPath)) { + // Get info on the original and the created variant + $originalFileSize = @filesize($tempPath); + $variantFileSize = @filesize($outputPath); - // Copy the image variant into place - $this->copyImageVariantToVolume( - $imageVariantCreators[$variantCreator], - $asset, - $index, - $outputPath - ); - } + Craft::info( + pathinfo($tempPath, PATHINFO_FILENAME) + .'.' + .pathinfo($tempPath, PATHINFO_EXTENSION) + .' -> ' + .pathinfo($outputPath, PATHINFO_FILENAME) + .'.' + .pathinfo($outputPath, PATHINFO_EXTENSION) + .' -> ' + .Craft::t('image-optimize', 'Original') + .': ' + .$this->humanFileSize($originalFileSize, 1) + .', ' + .Craft::t('image-optimize', 'Variant') + .': ' + .$this->humanFileSize($variantFileSize, 1) + .' -> ' + .Craft::t('image-optimize', 'Savings') + .': ' + .number_format(abs(100 - (($variantFileSize * 100) / $originalFileSize)), 1) + .'%', + __METHOD__ + ); + + // Copy the image variant into place + $this->copyImageVariantToVolume( + $imageVariantCreators[$variantCreator], + $asset, + $index, + $outputPath + ); } } } @@ -340,9 +336,9 @@ public function getActiveImageProcessors(): array if (!empty($imageProcessors[$processor])) { $thisImageProcessor = $imageProcessors[$processor]; $result[] = [ - 'format' => $imageFormat, - 'creator' => $processor, - 'command' => $thisImageProcessor['commandPath'] + 'format' => $imageFormat, + 'creator' => $processor, + 'command' => $thisImageProcessor['commandPath'] .' ' .$thisImageProcessor['commandOptions'], 'installed' => is_file($thisImageProcessor['commandPath']), @@ -372,9 +368,9 @@ public function getActiveVariantCreators(): array if (!empty($imageVariantCreators[$variantCreator])) { $thisVariantCreator = $imageVariantCreators[$variantCreator]; $result[] = [ - 'format' => $imageFormat, - 'creator' => $variantCreator, - 'command' => $thisVariantCreator['commandPath'] + 'format' => $imageFormat, + 'creator' => $variantCreator, + 'command' => $thisVariantCreator['commandPath'] .' ' .$thisVariantCreator['commandOptions'], 'installed' => is_file($thisVariantCreator['commandPath']), @@ -585,9 +581,9 @@ protected function cleanupImageVariants(Asset $asset, AssetTransformIndex $trans } try { $variantPath = $asset->getFolder()->path.$assetTransforms->getTransformSubpath( - $asset, - $transformIndex - ); + $asset, + $transformIndex + ); } catch (InvalidConfigException $e) { $variantPath = ''; Craft::error( From 0cf766c6ed5c2038950f4202449433331e9ad4af Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 24 May 2018 18:53:20 -0400 Subject: [PATCH 3/4] Version 1.4.33 Signed-off-by: Andrew Welch --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90f3f9ad..7670ded9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # ImageOptimize Changelog +## 1.4.33 - 2018.05.24 +### Changed +* Handle JPEGs coming in as both `jpg` & `jpeg` + ## 1.4.32 - 2018.05.09 ### Added * Improved CraftQL support by allowing parameter passing to `src` and `srcUrls` diff --git a/composer.json b/composer.json index 923a50c4..4c660de2 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-imageoptimize", "description": "Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.", "type": "craft-plugin", - "version": "1.4.32", + "version": "1.4.33", "keywords": [ "craft", "cms", From a70d971dc6a5c351372f206d9bd5749b9e8a13fe Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 24 May 2018 18:55:09 -0400 Subject: [PATCH 4/4] Version 1.4.33 Signed-off-by: Andrew Welch --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7670ded9..26927918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 1.4.33 - 2018.05.24 ### Changed -* Handle JPEGs coming in as both `jpg` & `jpeg` +* Handle JPEGs coming in as both `jpg` & `jpeg` for the detected file format +* Remove vestigal `empty()` checks ## 1.4.32 - 2018.05.09 ### Added