Created
December 30, 2014 19:21
-
-
Save Schrank/a793f360740803ded773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Varien_Image_Adapter_Gd2 extends Varien_Image_Adapter_Abstract | |
{ | |
protected $_requiredExtensions = Array("gd"); | |
private static $_callbacks = array( | |
IMAGETYPE_GIF => array('output' => 'imagegif', 'create' => 'imagecreatefromgif'), | |
IMAGETYPE_JPEG => array('output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'), | |
IMAGETYPE_PNG => array('output' => 'imagepng', 'create' => 'imagecreatefrompng'), | |
IMAGETYPE_XBM => array('output' => 'imagexbm', 'create' => 'imagecreatefromxbm'), | |
IMAGETYPE_WBMP => array('output' => 'imagewbmp', 'create' => 'imagecreatefromxbm'), | |
); | |
/** | |
* Whether image was resized or not | |
* | |
* @var bool | |
*/ | |
protected $_resized = false; | |
public function open($filename) | |
{ | |
$this->_fileName = $filename; | |
$this->getMimeType(); | |
$this->_getFileAttributes(); | |
$this->_imageHandler = call_user_func($this->_getCallback('create'), $this->_fileName); | |
} | |
public function save($destination=null, $newName=null) | |
{ | |
$fileName = ( !isset($destination) ) ? $this->_fileName : $destination; | |
if( isset($destination) && isset($newName) ) { | |
$fileName = $destination . "/" . $newName; | |
} elseif( isset($destination) && !isset($newName) ) { | |
$info = pathinfo($destination); | |
$fileName = $destination; | |
$destination = $info['dirname']; | |
} elseif( !isset($destination) && isset($newName) ) { | |
$fileName = $this->_fileSrcPath . "/" . $newName; | |
} else { | |
$fileName = $this->_fileSrcPath . $this->_fileSrcName; | |
} | |
$destinationDir = ( isset($destination) ) ? $destination : $this->_fileSrcPath; | |
if( !is_writable($destinationDir) ) { | |
try { | |
$io = new Varien_Io_File(); | |
$io->mkdir($destination); | |
} catch (Exception $e) { | |
throw new Exception("Unable to write file into directory '{$destinationDir}'. Access forbidden."); | |
} | |
} | |
if (!$this->_resized) { | |
// keep alpha transparency | |
$isAlpha = false; | |
$isTrueColor = false; | |
$this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor); | |
if ($isAlpha) { | |
if ($isTrueColor) { | |
$newImage = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight); | |
} else { | |
$newImage = imagecreate($this->_imageSrcWidth, $this->_imageSrcHeight); | |
} | |
$this->_fillBackgroundColor($newImage); | |
imagecopy( | |
$newImage, | |
$this->_imageHandler, | |
0, 0, | |
0, 0, | |
$this->_imageSrcWidth, $this->_imageSrcHeight | |
); | |
$this->_imageHandler = $newImage; | |
} | |
} | |
$functionParameters = array(); | |
$functionParameters[] = $this->_imageHandler; | |
$functionParameters[] = $fileName; | |
// set quality param for JPG file type | |
if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_JPEG) | |
{ | |
$functionParameters[] = $this->quality(); | |
} | |
// set quality param for PNG file type | |
if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_PNG) | |
{ | |
$quality = round(($this->quality() / 100) * 10); | |
if ($quality < 1) { | |
$quality = 1; | |
} elseif ($quality > 10) { | |
$quality = 10; | |
} | |
$quality = 10 - $quality; | |
$functionParameters[] = $quality; | |
} | |
call_user_func_array($this->_getCallback('output'), $functionParameters); | |
} | |
public function display() | |
{ | |
header("Content-type: ".$this->getMimeType()); | |
call_user_func($this->_getCallback('output'), $this->_imageHandler); | |
} | |
/** | |
* Obtain function name, basing on image type and callback type | |
* | |
* @param string $callbackType | |
* @param int $fileType | |
* @return string | |
* @throws Exception | |
*/ | |
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.') | |
{ | |
if (null === $fileType) { | |
$fileType = $this->_fileType; | |
} | |
if (empty(self::$_callbacks[$fileType])) { | |
throw new Exception($unsupportedText); | |
} | |
if (empty(self::$_callbacks[$fileType][$callbackType])) { | |
throw new Exception('Callback not found.'); | |
} | |
return self::$_callbacks[$fileType][$callbackType]; | |
} | |
private function _fillBackgroundColor(&$imageResourceTo) | |
{ | |
// try to keep transparency, if any | |
if ($this->_keepTransparency) { | |
$isAlpha = false; | |
$transparentIndex = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha); | |
try { | |
// fill truecolor png with alpha transparency | |
if ($isAlpha) { | |
if (!imagealphablending($imageResourceTo, false)) { | |
throw new Exception('Failed to set alpha blending for PNG image.'); | |
} | |
$transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127); | |
if (false === $transparentAlphaColor) { | |
throw new Exception('Failed to allocate alpha transparency for PNG image.'); | |
} | |
if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) { | |
throw new Exception('Failed to fill PNG image with alpha transparency.'); | |
} | |
if (!imagesavealpha($imageResourceTo, true)) { | |
throw new Exception('Failed to save alpha transparency into PNG image.'); | |
} | |
return $transparentAlphaColor; | |
} | |
// fill image with indexed non-alpha transparency | |
elseif (false !== $transparentIndex) { | |
$transparentColor = false; | |
if ($transparentIndex >=0 && $transparentIndex <= imagecolorstotal($this->_imageHandler)) { | |
list($r, $g, $b) = array_values(imagecolorsforindex($this->_imageHandler, $transparentIndex)); | |
$transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b); | |
} | |
if (false === $transparentColor) { | |
throw new Exception('Failed to allocate transparent color for image.'); | |
} | |
if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) { | |
throw new Exception('Failed to fill image with transparency.'); | |
} | |
imagecolortransparent($imageResourceTo, $transparentColor); | |
return $transparentColor; | |
} | |
} | |
catch (Exception $e) { | |
// fallback to default background color | |
} | |
} | |
list($r, $g, $b) = $this->_backgroundColor; | |
$color = imagecolorallocate($imageResourceTo, $r, $g, $b); | |
if (!imagefill($imageResourceTo, 0, 0, $color)) { | |
throw new Exception("Failed to fill image background with color {$r} {$g} {$b}."); | |
} | |
return $color; | |
} | |
/** | |
* Gives true for a PNG with alpha, false otherwise | |
* | |
* @param string $fileName | |
* @return boolean | |
*/ | |
public function checkAlpha($fileName) | |
{ | |
return ((ord(file_get_contents($fileName, false, null, 25, 1)) & 6) & 4) == 4; | |
} | |
private function _getTransparency($imageResource, $fileType, &$isAlpha = false, &$isTrueColor = false) | |
{ | |
$isAlpha = false; | |
$isTrueColor = false; | |
// assume that transparency is supported by gif/png only | |
if ((IMAGETYPE_GIF === $fileType) || (IMAGETYPE_PNG === $fileType)) { | |
// check for specific transparent color | |
$transparentIndex = imagecolortransparent($imageResource); | |
if ($transparentIndex >= 0) { | |
return $transparentIndex; | |
} | |
// assume that truecolor PNG has transparency | |
elseif (IMAGETYPE_PNG === $fileType) { | |
$isAlpha = $this->checkAlpha($this->_fileName); | |
$isTrueColor = true; | |
return $transparentIndex; // -1 | |
} | |
} | |
if (IMAGETYPE_JPEG === $fileType) { | |
$isTrueColor = true; | |
} | |
return false; | |
} | |
/** | |
* Change the image size | |
* | |
* @param int $frameWidth | |
* @param int $frameHeight | |
*/ | |
public function resize($frameWidth = null, $frameHeight = null) | |
{ | |
if (empty($frameWidth) && empty($frameHeight)) { | |
throw new Exception('Invalid image dimensions.'); | |
} | |
// calculate lacking dimension | |
if (!$this->_keepFrame) { | |
if (null === $frameWidth) { | |
$frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight)); | |
} | |
elseif (null === $frameHeight) { | |
$frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth)); | |
} | |
} | |
else { | |
if (null === $frameWidth) { | |
$frameWidth = $frameHeight; | |
} | |
elseif (null === $frameHeight) { | |
$frameHeight = $frameWidth; | |
} | |
} | |
// define coordinates of image inside new frame | |
$srcX = 0; | |
$srcY = 0; | |
$dstX = 0; | |
$dstY = 0; | |
$dstWidth = $frameWidth; | |
$dstHeight = $frameHeight; | |
if ($this->_keepAspectRatio) { | |
// do not make picture bigger, than it is, if required | |
if ($this->_constrainOnly) { | |
if (($frameWidth >= $this->_imageSrcWidth) && ($frameHeight >= $this->_imageSrcHeight)) { | |
$dstWidth = $this->_imageSrcWidth; | |
$dstHeight = $this->_imageSrcHeight; | |
} | |
} | |
// keep aspect ratio | |
if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $frameWidth / $frameHeight) { | |
$dstHeight = round(($dstWidth / $this->_imageSrcWidth) * $this->_imageSrcHeight); | |
} else { | |
$dstWidth = round(($dstHeight / $this->_imageSrcHeight) * $this->_imageSrcWidth); | |
} | |
} | |
// define position in center (TODO: add positions option) | |
$dstY = round(($frameHeight - $dstHeight) / 2); | |
$dstX = round(($frameWidth - $dstWidth) / 2); | |
// get rid of frame (fallback to zero position coordinates) | |
if (!$this->_keepFrame) { | |
$frameWidth = $dstWidth; | |
$frameHeight = $dstHeight; | |
$dstY = 0; | |
$dstX = 0; | |
} | |
// create new image | |
$isAlpha = false; | |
$isTrueColor = false; | |
$this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor); | |
if ($isTrueColor) { | |
$newImage = imagecreatetruecolor($frameWidth, $frameHeight); | |
} | |
else { | |
$newImage = imagecreate($frameWidth, $frameHeight); | |
} | |
// fill new image with required color | |
$this->_fillBackgroundColor($newImage); | |
// resample source image and copy it into new frame | |
imagecopyresampled( | |
$newImage, | |
$this->_imageHandler, | |
$dstX, $dstY, | |
$srcX, $srcY, | |
$dstWidth, $dstHeight, | |
$this->_imageSrcWidth, $this->_imageSrcHeight | |
); | |
$this->_imageHandler = $newImage; | |
$this->refreshImageDimensions(); | |
$this->_resized = true; | |
} | |
public function rotate($angle) | |
{ | |
/* | |
$isAlpha = false; | |
$backgroundColor = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha); | |
list($r, $g, $b) = $this->_backgroundColor; | |
if ($isAlpha) { | |
$backgroundColor = imagecolorallocatealpha($this->_imageHandler, 0, 0, 0, 127); | |
} | |
elseif (false === $backgroundColor) { | |
$backgroundColor = imagecolorallocate($this->_imageHandler, $r, $g, $b); | |
} | |
$this->_imageHandler = imagerotate($this->_imageHandler, $angle, $backgroundColor); | |
//*/ | |
$this->_imageHandler = imagerotate($this->_imageHandler, $angle, $this->imageBackgroundColor); | |
$this->refreshImageDimensions(); | |
} | |
public function watermark($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false) | |
{ | |
list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = getimagesize($watermarkImage); | |
$this->_getFileAttributes(); | |
$watermark = call_user_func($this->_getCallback( | |
'create', | |
$watermarkFileType, | |
'Unsupported watermark image format.' | |
), $watermarkImage); | |
$merged = false; | |
if ($this->getWatermarkWidth() && | |
$this->getWatermarkHeigth() && | |
($this->getWatermarkPosition() != self::POSITION_STRETCH) | |
) { | |
$newWatermark = imagecreatetruecolor($this->getWatermarkWidth(), $this->getWatermarkHeigth()); | |
imagealphablending($newWatermark, false); | |
$col = imagecolorallocate($newWatermark, 255, 255, 255); | |
imagecolortransparent($newWatermark, $col); | |
imagefilledrectangle($newWatermark, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), $col); | |
imagealphablending($newWatermark, true); | |
imageSaveAlpha($newWatermark, true); | |
imagecopyresampled( | |
$newWatermark, | |
$watermark, | |
0, 0, 0, 0, | |
$this->getWatermarkWidth(), $this->getWatermarkHeigth(), | |
imagesx($watermark), imagesy($watermark) | |
); | |
$watermark = $newWatermark; | |
} | |
if( $this->getWatermarkPosition() == self::POSITION_TILE ) { | |
$repeat = true; | |
} elseif( $this->getWatermarkPosition() == self::POSITION_STRETCH ) { | |
$newWatermark = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight); | |
imagealphablending($newWatermark, false); | |
$col = imagecolorallocate($newWatermark, 255, 255, 255); | |
imagecolortransparent($newWatermark, $col); | |
imagefilledrectangle($newWatermark, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, $col); | |
imagealphablending($newWatermark, true); | |
imageSaveAlpha($newWatermark, true); | |
imagecopyresampled( | |
$newWatermark, | |
$watermark, | |
0, 0, 0, 0, | |
$this->_imageSrcWidth, $this->_imageSrcHeight, | |
imagesx($watermark), imagesy($watermark) | |
); | |
$watermark = $newWatermark; | |
} elseif( $this->getWatermarkPosition() == self::POSITION_CENTER ) { | |
$positionX = ($this->_imageSrcWidth/2 - imagesx($watermark)/2); | |
$positionY = ($this->_imageSrcHeight/2 - imagesy($watermark)/2); | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$positionX, $positionY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
} elseif( $this->getWatermarkPosition() == self::POSITION_TOP_RIGHT ) { | |
$positionX = ($this->_imageSrcWidth - imagesx($watermark)); | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$positionX, $positionY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
} elseif( $this->getWatermarkPosition() == self::POSITION_TOP_LEFT ) { | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$positionX, $positionY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
} elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT ) { | |
$positionX = ($this->_imageSrcWidth - imagesx($watermark)); | |
$positionY = ($this->_imageSrcHeight - imagesy($watermark)); | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$positionX, $positionY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
} elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT ) { | |
$positionY = ($this->_imageSrcHeight - imagesy($watermark)); | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$positionX, $positionY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
} | |
if( $repeat === false && $merged === false ) { | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$positionX, $positionY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
} else { | |
$offsetX = $positionX; | |
$offsetY = $positionY; | |
while( $offsetY <= ($this->_imageSrcHeight+imagesy($watermark)) ) { | |
while( $offsetX <= ($this->_imageSrcWidth+imagesx($watermark)) ) { | |
imagecopymerge( | |
$this->_imageHandler, | |
$watermark, | |
$offsetX, $offsetY, | |
0, 0, | |
imagesx($watermark), imagesy($watermark), | |
$this->getWatermarkImageOpacity() | |
); | |
$offsetX += imagesx($watermark); | |
} | |
$offsetX = $positionX; | |
$offsetY += imagesy($watermark); | |
} | |
} | |
imagedestroy($watermark); | |
$this->refreshImageDimensions(); | |
} | |
public function crop($top=0, $left=0, $right=0, $bottom=0) | |
{ | |
if( $left == 0 && $top == 0 && $right == 0 && $bottom == 0 ) { | |
return; | |
} | |
$newWidth = $this->_imageSrcWidth - $left - $right; | |
$newHeight = $this->_imageSrcHeight - $top - $bottom; | |
$canvas = imagecreatetruecolor($newWidth, $newHeight); | |
if ($this->_fileType == IMAGETYPE_PNG) { | |
$this->_saveAlpha($canvas); | |
} | |
imagecopyresampled( | |
$canvas, | |
$this->_imageHandler, | |
0, 0, $left, $top, | |
$newWidth, $newHeight, | |
$newWidth, $newHeight | |
); | |
$this->_imageHandler = $canvas; | |
$this->refreshImageDimensions(); | |
} | |
public function checkDependencies() | |
{ | |
foreach( $this->_requiredExtensions as $value ) { | |
if( !extension_loaded($value) ) { | |
throw new Exception("Required PHP extension '{$value}' was not loaded."); | |
} | |
} | |
} | |
private function refreshImageDimensions() | |
{ | |
$this->_imageSrcWidth = imagesx($this->_imageHandler); | |
$this->_imageSrcHeight = imagesy($this->_imageHandler); | |
} | |
function __destruct() | |
{ | |
@imagedestroy($this->_imageHandler); | |
} | |
/* | |
* Fixes saving PNG alpha channel | |
*/ | |
private function _saveAlpha($imageHandler) | |
{ | |
$background = imagecolorallocate($imageHandler, 0, 0, 0); | |
ImageColorTransparent($imageHandler, $background); | |
imagealphablending($imageHandler, false); | |
imagesavealpha($imageHandler, true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Catalog image helper | |
* | |
* @author Magento Core Team <[email protected]> | |
*/ | |
class Mage_Catalog_Helper_Image extends Mage_Core_Helper_Abstract | |
{ | |
/** | |
* Current model | |
* | |
* @var Mage_Catalog_Model_Product_Image | |
*/ | |
protected $_model; | |
/** | |
* Scheduled for resize image | |
* | |
* @var bool | |
*/ | |
protected $_scheduleResize = false; | |
/** | |
* Scheduled for rotate image | |
* | |
* @var bool | |
*/ | |
protected $_scheduleRotate = false; | |
/** | |
* Angle | |
* | |
* @var int | |
*/ | |
protected $_angle; | |
/** | |
* Watermark file name | |
* | |
* @var string | |
*/ | |
protected $_watermark; | |
/** | |
* Watermark Position | |
* | |
* @var string | |
*/ | |
protected $_watermarkPosition; | |
/** | |
* Watermark Size | |
* | |
* @var string | |
*/ | |
protected $_watermarkSize; | |
/** | |
* Watermark Image opacity | |
* | |
* @var int | |
*/ | |
protected $_watermarkImageOpacity; | |
/** | |
* Current Product | |
* | |
* @var Mage_Catalog_Model_Product | |
*/ | |
protected $_product; | |
/** | |
* Image File | |
* | |
* @var string | |
*/ | |
protected $_imageFile; | |
/** | |
* Image Placeholder | |
* | |
* @var string | |
*/ | |
protected $_placeholder; | |
/** | |
* Reset all previous data | |
* | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function _reset() | |
{ | |
$this->_model = null; | |
$this->_scheduleResize = false; | |
$this->_scheduleRotate = false; | |
$this->_angle = null; | |
$this->_watermark = null; | |
$this->_watermarkPosition = null; | |
$this->_watermarkSize = null; | |
$this->_watermarkImageOpacity = null; | |
$this->_product = null; | |
$this->_imageFile = null; | |
return $this; | |
} | |
/** | |
* Initialize Helper to work with Image | |
* | |
* @param Mage_Catalog_Model_Product $product | |
* @param string $attributeName | |
* @param mixed $imageFile | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null) | |
{ | |
$this->_reset(); | |
$this->_setModel(Mage::getModel('catalog/product_image')); | |
$this->_getModel()->setDestinationSubdir($attributeName); | |
$this->setProduct($product); | |
$this->setWatermark( | |
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_image") | |
); | |
$this->setWatermarkImageOpacity( | |
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_imageOpacity") | |
); | |
$this->setWatermarkPosition( | |
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_position") | |
); | |
$this->setWatermarkSize( | |
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_size") | |
); | |
if ($imageFile) { | |
$this->setImageFile($imageFile); | |
} else { | |
// add for work original size | |
$this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir())); | |
} | |
return $this; | |
} | |
/** | |
* Schedule resize of the image | |
* $width *or* $height can be null - in this case, lacking dimension will be calculated. | |
* | |
* @see Mage_Catalog_Model_Product_Image | |
* @param int $width | |
* @param int $height | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function resize($width, $height = null) | |
{ | |
$this->_getModel()->setWidth($width)->setHeight($height); | |
$this->_scheduleResize = true; | |
return $this; | |
} | |
/** | |
* Set image quality, values in percentage from 0 to 100 | |
* | |
* @param int $quality | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function setQuality($quality) | |
{ | |
$this->_getModel()->setQuality($quality); | |
return $this; | |
} | |
/** | |
* Guarantee, that image picture width/height will not be distorted. | |
* Applicable before calling resize() | |
* It is true by default. | |
* | |
* @see Mage_Catalog_Model_Product_Image | |
* @param bool $flag | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function keepAspectRatio($flag) | |
{ | |
$this->_getModel()->setKeepAspectRatio($flag); | |
return $this; | |
} | |
/** | |
* Guarantee, that image will have dimensions, set in $width/$height | |
* Applicable before calling resize() | |
* Not applicable, if keepAspectRatio(false) | |
* | |
* $position - TODO, not used for now - picture position inside the frame. | |
* | |
* @see Mage_Catalog_Model_Product_Image | |
* @param bool $flag | |
* @param array $position | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function keepFrame($flag, $position = array('center', 'middle')) | |
{ | |
$this->_getModel()->setKeepFrame($flag); | |
return $this; | |
} | |
/** | |
* Guarantee, that image will not lose transparency if any. | |
* Applicable before calling resize() | |
* It is true by default. | |
* | |
* $alphaOpacity - TODO, not used for now | |
* | |
* @see Mage_Catalog_Model_Product_Image | |
* @param bool $flag | |
* @param int $alphaOpacity | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function keepTransparency($flag, $alphaOpacity = null) | |
{ | |
$this->_getModel()->setKeepTransparency($flag); | |
return $this; | |
} | |
/** | |
* Guarantee, that image picture will not be bigger, than it was. | |
* Applicable before calling resize() | |
* It is false by default | |
* | |
* @param bool $flag | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function constrainOnly($flag) | |
{ | |
$this->_getModel()->setConstrainOnly($flag); | |
return $this; | |
} | |
/** | |
* Set color to fill image frame with. | |
* Applicable before calling resize() | |
* The keepTransparency(true) overrides this (if image has transparent color) | |
* It is white by default. | |
* | |
* @see Mage_Catalog_Model_Product_Image | |
* @param array $colorRGB | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function backgroundColor($colorRGB) | |
{ | |
// assume that 3 params were given instead of array | |
if (!is_array($colorRGB)) { | |
$colorRGB = func_get_args(); | |
} | |
$this->_getModel()->setBackgroundColor($colorRGB); | |
return $this; | |
} | |
/** | |
* Rotate image into specified angle | |
* | |
* @param int $angle | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function rotate($angle) | |
{ | |
$this->setAngle($angle); | |
$this->_getModel()->setAngle($angle); | |
$this->_scheduleRotate = true; | |
return $this; | |
} | |
/** | |
* Add watermark to image | |
* size param in format 100x200 | |
* | |
* @param string $fileName | |
* @param string $position | |
* @param string $size | |
* @param int $imageOpacity | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function watermark($fileName, $position, $size=null, $imageOpacity=null) | |
{ | |
$this->setWatermark($fileName) | |
->setWatermarkPosition($position) | |
->setWatermarkSize($size) | |
->setWatermarkImageOpacity($imageOpacity); | |
return $this; | |
} | |
/** | |
* Set placeholder | |
* | |
* @param string $fileName | |
* @return void | |
*/ | |
public function placeholder($fileName) | |
{ | |
$this->_placeholder = $fileName; | |
} | |
/** | |
* Get Placeholder | |
* | |
* @return string | |
*/ | |
public function getPlaceholder() | |
{ | |
if (!$this->_placeholder) { | |
$attr = $this->_getModel()->getDestinationSubdir(); | |
$this->_placeholder = 'images/catalog/product/placeholder/'.$attr.'.jpg'; | |
} | |
return $this->_placeholder; | |
} | |
/** | |
* Return Image URL | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
try { | |
$model = $this->_getModel(); | |
if ($this->getImageFile()) { | |
$model->setBaseFile($this->getImageFile()); | |
} else { | |
$model->setBaseFile($this->getProduct()->getData($model->getDestinationSubdir())); | |
} | |
if ($model->isCached()) { | |
return $model->getUrl(); | |
} else { | |
if ($this->_scheduleRotate) { | |
$model->rotate($this->getAngle()); | |
} | |
if ($this->_scheduleResize) { | |
$model->resize(); | |
} | |
if ($this->getWatermark()) { | |
$model->setWatermark($this->getWatermark()); | |
} | |
$url = $model->saveFile()->getUrl(); | |
} | |
} catch (Exception $e) { | |
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder()); | |
} | |
return $url; | |
} | |
/** | |
* Set current Image model | |
* | |
* @param Mage_Catalog_Model_Product_Image $model | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function _setModel($model) | |
{ | |
$this->_model = $model; | |
return $this; | |
} | |
/** | |
* Get current Image model | |
* | |
* @return Mage_Catalog_Model_Product_Image | |
*/ | |
protected function _getModel() | |
{ | |
return $this->_model; | |
} | |
/** | |
* Set Rotation Angle | |
* | |
* @param int $angle | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function setAngle($angle) | |
{ | |
$this->_angle = $angle; | |
return $this; | |
} | |
/** | |
* Get Rotation Angle | |
* | |
* @return int | |
*/ | |
protected function getAngle() | |
{ | |
return $this->_angle; | |
} | |
/** | |
* Set watermark file name | |
* | |
* @param string $watermark | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function setWatermark($watermark) | |
{ | |
$this->_watermark = $watermark; | |
$this->_getModel()->setWatermarkFile($watermark); | |
return $this; | |
} | |
/** | |
* Get watermark file name | |
* | |
* @return string | |
*/ | |
protected function getWatermark() | |
{ | |
return $this->_watermark; | |
} | |
/** | |
* Set watermark position | |
* | |
* @param string $position | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function setWatermarkPosition($position) | |
{ | |
$this->_watermarkPosition = $position; | |
$this->_getModel()->setWatermarkPosition($position); | |
return $this; | |
} | |
/** | |
* Get watermark position | |
* | |
* @return string | |
*/ | |
protected function getWatermarkPosition() | |
{ | |
return $this->_watermarkPosition; | |
} | |
/** | |
* Set watermark size | |
* param size in format 100x200 | |
* | |
* @param string $size | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function setWatermarkSize($size) | |
{ | |
$this->_watermarkSize = $size; | |
$this->_getModel()->setWatermarkSize($this->parseSize($size)); | |
return $this; | |
} | |
/** | |
* Get watermark size | |
* | |
* @return string | |
*/ | |
protected function getWatermarkSize() | |
{ | |
return $this->_watermarkSize; | |
} | |
/** | |
* Set watermark image opacity | |
* | |
* @param int $imageOpacity | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
public function setWatermarkImageOpacity($imageOpacity) | |
{ | |
$this->_watermarkImageOpacity = $imageOpacity; | |
$this->_getModel()->setWatermarkImageOpacity($imageOpacity); | |
return $this; | |
} | |
/** | |
* Get watermark image opacity | |
* | |
* @return int | |
*/ | |
protected function getWatermarkImageOpacity() | |
{ | |
if ($this->_watermarkImageOpacity) { | |
return $this->_watermarkImageOpacity; | |
} | |
return $this->_getModel()->getWatermarkImageOpacity(); | |
} | |
/** | |
* Set current Product | |
* | |
* @param Mage_Catalog_Model_Product $product | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function setProduct($product) | |
{ | |
$this->_product = $product; | |
return $this; | |
} | |
/** | |
* Get current Product | |
* | |
* @return Mage_Catalog_Model_Product | |
*/ | |
protected function getProduct() | |
{ | |
return $this->_product; | |
} | |
/** | |
* Set Image file | |
* | |
* @param string $file | |
* @return Mage_Catalog_Helper_Image | |
*/ | |
protected function setImageFile($file) | |
{ | |
$this->_imageFile = $file; | |
return $this; | |
} | |
/** | |
* Get Image file | |
* | |
* @return string | |
*/ | |
protected function getImageFile() | |
{ | |
return $this->_imageFile; | |
} | |
/** | |
* Retrieve size from string | |
* | |
* @param string $string | |
* @return array|bool | |
*/ | |
protected function parseSize($string) | |
{ | |
$size = explode('x', strtolower($string)); | |
if (sizeof($size) == 2) { | |
return array( | |
'width' => ($size[0] > 0) ? $size[0] : null, | |
'heigth' => ($size[1] > 0) ? $size[1] : null, | |
); | |
} | |
return false; | |
} | |
/** | |
* Retrieve original image width | |
* | |
* @return int|null | |
*/ | |
public function getOriginalWidth() | |
{ | |
return $this->_getModel()->getImageProcessor()->getOriginalWidth(); | |
} | |
/** | |
* Retrieve original image height | |
* | |
* @deprecated | |
* @return int|null | |
*/ | |
public function getOriginalHeigh() | |
{ | |
return $this->getOriginalHeight(); | |
} | |
/** | |
* Retrieve original image height | |
* | |
* @return int|null | |
*/ | |
public function getOriginalHeight() | |
{ | |
return $this->_getModel()->getImageProcessor()->getOriginalHeight(); | |
} | |
/** | |
* Retrieve Original image size as array | |
* 0 - width, 1 - height | |
* | |
* @return array | |
*/ | |
public function getOriginalSizeArray() | |
{ | |
return array( | |
$this->getOriginalWidth(), | |
$this->getOriginalHeight() | |
); | |
} | |
/** | |
* Check - is this file an image | |
* | |
* @param string $filePath | |
* @return bool | |
* @throws Mage_Core_Exception | |
*/ | |
public function validateUploadFile($filePath) { | |
if (!getimagesize($filePath)) { | |
Mage::throwException($this->__('Disallowed file type.')); | |
} | |
$_processor = new Varien_Image($filePath); | |
return $_processor->getMimeType() !== null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment