From f3ce95c2a435e0a660f10cee7579636ed7561d1c Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Tue, 12 Apr 2016 15:58:26 -0400 Subject: [PATCH 1/7] Explicitly set temporary-path to prevent file access errors in auditd. --- src/app/code/community/Varien/Image/Adapter/Imagemagic.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php index bf77b88..e89d291 100644 --- a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php +++ b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php @@ -16,6 +16,8 @@ class Varien_Image_Adapter_Imagemagic extends Varien_Image_Adapter_Abstract protected function getImageMagick() { if ($this->_imageHandler === null) { + // Set tmp path since Imagick apparently does not choose it well (according to auditd file access errors) + Imagick::setRegistry('temporary-path', Mage::getBaseDir('tmp')); $this->_imageHandler = new Imagick(); if ($threadLimit = Mage::getStoreConfig('design/watermark_adapter/thread_limit')) { $this->_imageHandler->setResourceLimit(6,max(1,min((int)$threadLimit,24))); // No constant available for threads From fe2d6a9fb6456eb5912702dd9a42dd5b6509cd16 Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Tue, 19 Apr 2016 17:25:17 -0400 Subject: [PATCH 2/7] Chdir to tmp directory when using old imagick. noref --- src/app/code/community/Varien/Image/Adapter/Imagemagic.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php index e89d291..34b1d50 100644 --- a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php +++ b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php @@ -18,6 +18,10 @@ protected function getImageMagick() if ($this->_imageHandler === null) { // Set tmp path since Imagick apparently does not choose it well (according to auditd file access errors) Imagick::setRegistry('temporary-path', Mage::getBaseDir('tmp')); + $version = Imagick::getVersion(); + if (strpos($version['versionString'], 'ImageMagick 6.7.') === 0) { + chdir(Mage::getBaseDir('tmp')); // Old versions don't use temporary-path but instead the cwd + } $this->_imageHandler = new Imagick(); if ($threadLimit = Mage::getStoreConfig('design/watermark_adapter/thread_limit')) { $this->_imageHandler->setResourceLimit(6,max(1,min((int)$threadLimit,24))); // No constant available for threads From fa7e19b0406678dec35ca399c77e18e6ae27afe2 Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Wed, 4 May 2016 17:59:36 -0400 Subject: [PATCH 3/7] Do not load image file if it is not one of specified types (Addresses Imagick RCE vulnerability). --- src/app/code/community/Varien/Image/Adapter/Imagemagic.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php index 34b1d50..6f217b5 100644 --- a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php +++ b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php @@ -39,6 +39,9 @@ public function open($fileName) $this->_fileName = $fileName; $this->getMimeType(); $this->_getFileAttributes(); + if ( ! in_array($this->getMimeType(), ['image/png', 'image/jpeg', 'image/gif'])) { + throw new Varien_Exception('Unsupported image file: '.$this->getMimeType()); + } $this->getImageMagick()->readimage($fileName); Varien_Profiler::stop(__METHOD__); } From bae33c349345ee6762009869349f309df6f12f95 Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Fri, 13 May 2016 16:20:03 -0400 Subject: [PATCH 4/7] Fix regression due to broken core method. --- .../Varien/Image/Adapter/Imagemagic.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php index 6f217b5..dc59477 100644 --- a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php +++ b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php @@ -7,6 +7,12 @@ class Varien_Image_Adapter_Imagemagic extends Varien_Image_Adapter_Abstract protected $_requiredExtensions = array('imagick'); + protected $_allowedTypes = [ + 'image/png', + 'image/jpeg', + 'image/gif', + ]; + /** * Get the Imagemagick class. * @@ -30,17 +36,35 @@ protected function getImageMagick() return $this->_imageHandler; } + /** + * Overrides broken core method (returns string the first time and int the second time) + * + * @return null|string + * @throws Varien_Exception + */ + public function getMimeType() + { + if( ! $this->_fileMimeType ) { + list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType, ) = @getimagesize($this->_fileName); + if ( ! $this->_fileType) { + throw new Varien_Exception('Could not get image file type.'); + } + $this->_fileMimeType = image_type_to_mime_type($this->_fileType); + } + return $this->_fileMimeType; + } + /** * @param $fileName + * @throws Varien_Exception */ public function open($fileName) { Varien_Profiler::start(__METHOD__); $this->_fileName = $fileName; - $this->getMimeType(); $this->_getFileAttributes(); - if ( ! in_array($this->getMimeType(), ['image/png', 'image/jpeg', 'image/gif'])) { - throw new Varien_Exception('Unsupported image file: '.$this->getMimeType()); + if ( ! in_array($this->getMimeType(), $this->_allowedTypes)) { + throw new Varien_Exception('Unsupported image file type: '.$this->getMimeType()); } $this->getImageMagick()->readimage($fileName); Varien_Profiler::stop(__METHOD__); From f1e4552049ef1973919ceb761b63fa07a0206b75 Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Mon, 22 May 2017 22:03:01 -0400 Subject: [PATCH 5/7] Fix PHP fatal error for older versions. --- src/app/code/community/Varien/Image/Adapter/Imagemagic.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php index f8943a9..c12a85d 100644 --- a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php +++ b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php @@ -23,7 +23,9 @@ protected function getImageMagick() { if ($this->_imageHandler === null) { // Set tmp path since Imagick apparently does not choose it well (according to auditd file access errors) - Imagick::setRegistry('temporary-path', Mage::getBaseDir('tmp')); + if (method_exists('Imagick','setRegistry')) { + Imagick::setRegistry('temporary-path', Mage::getBaseDir('tmp')); + } $version = Imagick::getVersion(); if (strpos($version['versionString'], 'ImageMagick 6.7.') === 0) { chdir(Mage::getBaseDir('tmp')); // Old versions don't use temporary-path but instead the cwd From 1c037020f3c3b6856ac9375948951e902cf5df13 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Fri, 19 Feb 2021 17:29:57 +0000 Subject: [PATCH 6/7] fixes for undefined constants in imagick 7 --- .../community/Varien/Image/Adapter/Imagemagic.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php index c12a85d..b0088ed 100644 --- a/src/app/code/community/Varien/Image/Adapter/Imagemagic.php +++ b/src/app/code/community/Varien/Image/Adapter/Imagemagic.php @@ -171,7 +171,11 @@ public function resize($frameWidth = null, $frameHeight = null) } // Resize - $imagick->setimageinterpolatemethod(imagick::INTERPOLATE_BICUBIC); + if (defined('imagick::INTERPOLATE_BICUBIC')) { + $imagick->setimageinterpolatemethod(imagick::INTERPOLATE_BICUBIC); + } elseif (defined('imagick::INTERPOLATE_NEAREST_PIXEL')) { + $imagick->setimageinterpolatemethod(imagick::INTERPOLATE_NEAREST_PIXEL); + } $imagick->scaleimage($frameWidth, $frameHeight, true); // Fill desired canvas @@ -286,9 +290,11 @@ public function watermark( $watermark = new Imagick($watermarkImage); //better method to blow up small images. - $watermark->setimageinterpolatemethod( - Imagick::INTERPOLATE_NEARESTNEIGHBOR - ); + if (defined('imagick::INTERPOLATE_NEARESTNEIGHBOR')) { + $watermark->setimageinterpolatemethod(imagick::INTERPOLATE_NEARESTNEIGHBOR); + } elseif (defined('imagick::INTERPOLATE_NEAREST_PIXEL')) { + $watermark->setimageinterpolatemethod(imagick::INTERPOLATE_NEAREST_PIXEL); + } if ($this->_watermarkImageOpacity == null) { $opc = $watermarkImageOpacity; From dcc017c4612ef1f8bde03cba51280afe3372d00c Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Fri, 1 Jul 2022 19:08:53 +0100 Subject: [PATCH 7/7] Update composer.json --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 441b770..ccfe020 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,11 @@ { - "name": "magento-hackathon/perfect_watermarks", + "name": "colinmollenhour/perfect_watermarks", "type": "magento-module", - "description": "Replacement for Magento's GD2 image adapter with imagemagick", + "description": "Replacement for OpenMage's GD2 image adapter with imagemagick", "license": "OSL-3.0", "authors": [ { "name": "Karl Spies" }], - "homepage": "https://github.com/magento-hackathon/Perfect_Watermarks" + "homepage": "https://github.com/colinmollenhour/Perfect_Watermarks" }