Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security related bug fixes. #28

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
51 changes: 46 additions & 5 deletions src/app/code/community/Varien/Image/Adapter/Imagemagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -16,6 +22,14 @@ 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)
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
}
$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
Expand All @@ -24,15 +38,36 @@ 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(), $this->_allowedTypes)) {
throw new Varien_Exception('Unsupported image file type: '.$this->getMimeType());
}
$this->getImageMagick()->readimage($fileName);
Varien_Profiler::stop(__METHOD__);
}
Expand Down Expand Up @@ -136,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
Expand Down Expand Up @@ -251,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;
Expand Down