From 419e4949ac48f67f30f4d485fa6444113a287b2e Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Sat, 3 Oct 2015 17:03:58 +1000 Subject: [PATCH] version 0.0.7 Signed-off-by: Josh Crawford --- README.md | 5 ++ imageresizer/ImageResizerPlugin.php | 12 ++-- imageresizer/services/ImageResizerService.php | 66 ++++++++++--------- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index e48b3f3..92d5915 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,11 @@ Aspect ratio options are: ## Changelog +#### 0.0.7 + +- Fix to ensure images uploaded are both an image, and manipulatable. +- Better error-catching for resizing. + #### 0.0.6 - Added cropping option to Element Actions. diff --git a/imageresizer/ImageResizerPlugin.php b/imageresizer/ImageResizerPlugin.php index b283adc..bc1e107 100644 --- a/imageresizer/ImageResizerPlugin.php +++ b/imageresizer/ImageResizerPlugin.php @@ -14,7 +14,7 @@ public function getName() public function getVersion() { - return '0.0.6'; + return '0.0.7'; } public function getDeveloper() @@ -64,9 +64,13 @@ public function init() if (craft()->imageResizer->getSettings()->enabled) { - // Only process if it's a new asset being saved - and if its actually an image - if ($event->params['isNewAsset'] && $asset->kind == 'image') { - craft()->imageResizer->resize($asset); + // Only process if it's a new asset being saved. + if ($event->params['isNewAsset']) { + + // Is this a manipulatable image? + if (ImageHelper::isImageManipulatable(IOHelper::getExtension($asset->filename))) { + craft()->imageResizer->resize($asset); + } } } }); diff --git a/imageresizer/services/ImageResizerService.php b/imageresizer/services/ImageResizerService.php index 91f7b77..18563b3 100644 --- a/imageresizer/services/ImageResizerService.php +++ b/imageresizer/services/ImageResizerService.php @@ -18,46 +18,52 @@ public function getSettings() public function resize($asset) { - // Get the full path of the asset we want to resize - $path = $this->_getImagePath($asset); + try { + // Get the full path of the asset we want to resize + $path = $this->_getImagePath($asset); - // If the path is false, we're not allowed to modify images in the source - kill it! - if (!$path) { - return true; - } + // If the path is false, we're not allowed to modify images in the source - kill it! + if (!$path) { + return true; + } - $image = craft()->images->loadImage($path); + $image = craft()->images->loadImage($path); - // Our maximum width/height for assets from plugin settings - $imageWidth = $this->getSettings()->imageWidth; - $imageHeight = $this->getSettings()->imageHeight; + // Our maximum width/height for assets from plugin settings + $imageWidth = $this->getSettings()->imageWidth; + $imageHeight = $this->getSettings()->imageHeight; - // Lets check to see if this image needs resizing. Split into two steps to ensure - // proper aspect ratio is preserved and no upscaling occurs. - $hasResized = false; + // Lets check to see if this image needs resizing. Split into two steps to ensure + // proper aspect ratio is preserved and no upscaling occurs. + $hasResized = false; - if ($image->getWidth() > $imageWidth) { - $hasResized = true; - $this->_resizeImage($image, $imageWidth, null); - } + if ($image->getWidth() > $imageWidth) { + $hasResized = true; + $this->_resizeImage($image, $imageWidth, null); + } - if ($image->getHeight() > $imageHeight) { - $hasResized = true; - $this->_resizeImage($image, null, $imageHeight); - } + if ($image->getHeight() > $imageHeight) { + $hasResized = true; + $this->_resizeImage($image, null, $imageHeight); + } - if ($hasResized) { - // Set image quality - but normalise (for PNG)! - $quality = $this->_getImageQuality($asset); - $image->setQuality($quality); + if ($hasResized) { + // Set image quality - but normalise (for PNG)! + $quality = $this->_getImageQuality($asset); + $image->setQuality($quality); - $image->saveAs($path); + $image->saveAs($path); - // Update the asset record to reflect changes - $this->_updateAsset($asset, $image, $path); - } + // Update the asset record to reflect changes + $this->_updateAsset($asset, $image, $path); + } + + return $asset; + } catch (\Exception $e) { + ImageResizerPlugin::log($e->getMessage(), LogLevel::Error, true); - return $asset; + return false; + } } public function crop($asset, $x1, $x2, $y1, $y2)