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

Imagick adapter, handles color profiles better #1750

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Varien/Image/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function factory($adapter)
break;

case self::ADAPTER_IM:
return new Varien_Image_Adapter_Imagemagic();
return new Varien_Image_Adapter_Imagick();
break;

case self::ADAPTER_IME:
Expand Down
249 changes: 249 additions & 0 deletions lib/Varien/Image/Adapter/Imagick.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php

class Varien_Image_Adapter_Imagick extends Varien_Image_Adapter_Abstract
{

/**
* @var Imagick
*/
protected $_imageHandler;

Check failure on line 9 in lib/Varien/Image/Adapter/Imagick.php

View workflow job for this annotation

GitHub Actions / PHPStan / Analyze

PHPDoc type Imagick of property Varien_Image_Adapter_Imagick::$_imageHandler is not covariant with PHPDoc type GdImage|resource of overridden property Varien_Image_Adapter_Abstract::$_imageHandler.

/**
* @var string[]
*/
protected $_requiredExtensions = ["imagick"];

/**
* Whether image was resized or not
*
* @var bool
*/
protected $_resized = false;

public function __construct()
{
register_shutdown_function(array($this, 'destruct'));
}

/**
* Destroy object image on shutdown
*/
public function destruct()
{
if ($this->_imageHandler) {
$this->_imageHandler->destroy();
}
}

/**
* Opens image file.
*
* @param string $filename
* @throws ImagickException
* @throws Mage_Core_Exception
*/
public function open($filename)
{
$this->_fileName = $filename;
$this->getMimeType();
$this->_getFileAttributes();

$this->_imageHandler = new Imagick();
$this->_imageHandler->readImage($filename);

$orientation = $this->_imageHandler->getImageProperty('exif:Orientation');
if (!empty($orientation)) {
switch ($orientation) {
case 1:
// Do nothing
break;
case 3:
$this->_imageHandler->rotateImage('#000000', 180);
break;
case 6:
$this->_imageHandler->rotateImage('#000000', 90);
break;
case 8:
$this->_imageHandler->rotateImage('#000000', -90);
break;
default:
Mage::throwException('Unsupported EXIF orientation: ' . $orientation);
}
}
$this->refreshImageDimensions();
}

public function save($destination = null, $newName = null)
{
if ($destination && $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->_fileType == IMAGETYPE_JPEG) {
// $threshold = (int)Mage::getStoreConfig('catalog/product_image/progressive_threshold');
// if ($threshold && $threshold <= (imagesx($this->_imageHandler) * imagesy($this->_imageHandler) / 1000000)) {
// imageinterlace($this->_imageHandler, 1);
//
// TODO support progressive JPG? Like this?
// $this->_imageHandler->setInterlaceScheme();
// }
// }
fballiano marked this conversation as resolved.
Show resolved Hide resolved

// set quality param for PNG file type
fballiano marked this conversation as resolved.
Show resolved Hide resolved
$quality = $this->quality();
if ($quality !== null) {
if ($quality < 1) {
throw new RuntimeException('Image compression quality cannot be < 1');
} elseif ($quality > 100) {
throw new RuntimeException('Image compression quality cannot be > 100');
}
$this->_imageHandler->setCompressionQuality($quality);
}
$this->_imageHandler->writeImage($fileName);
}

public function display()
fballiano marked this conversation as resolved.
Show resolved Hide resolved
{
header("Content-type: " . $this->getMimeTypeWithOutFileType());
echo $this->_imageHandler->getImageBlob();
}

/**
* Change the image size
*
fballiano marked this conversation as resolved.
Show resolved Hide resolved
* @param int $frameWidth
* @param int $frameHeight
*/
public function resize($frameWidth = null, $frameHeight = null)
{
if (!$frameWidth && !$frameHeight) {
throw new RuntimeException('Invalid image dimensions.');
}

// calculate lacking dimension
if ($this->_keepFrame) {
if (null === $frameWidth) {
$frameWidth = $frameHeight;
} elseif (null === $frameHeight) {
$frameHeight = $frameWidth;
}
} else {
if (null === $frameWidth) {
$frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight));
} elseif (null === $frameHeight) {
$frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth));
}
}

// define coordinates of image inside new frame
$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 = ($dstWidth / $this->_imageSrcWidth) * $this->_imageSrcHeight;
} else {
$dstWidth = ($dstHeight / $this->_imageSrcHeight) * $this->_imageSrcWidth;
}
}

$dstWidth = (int)round($dstWidth);
$dstHeight = (int)round($dstHeight);
$frameWidth = (int)round($frameWidth);
$frameHeight = (int)round($frameHeight);


$filter = \Imagick::FILTER_LANCZOS;
$this->_imageHandler->resizeImage($dstWidth, $dstHeight, $filter, 1);

if ($this->_keepFrame) {
// Add borders top+bottom or left+right
$canvas = new Imagick();
// TODO support more than just JPG?
$canvas->newImage($frameWidth, $frameHeight, 'white', 'jpg');
$offsetX = (int)round(($frameWidth - $dstWidth) / 2);
$offsetY = (int)round(($frameHeight - $dstHeight) / 2);
$canvas->compositeImage($this->_imageHandler, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY);
$this->_imageHandler = $canvas;
}

$this->refreshImageDimensions();
$this->_resized = true;
}

public function rotate($angle)
fballiano marked this conversation as resolved.
Show resolved Hide resolved
{
$this->_imageHandler->rotateImage($this->imageBackgroundColor, $angle);
$this->refreshImageDimensions();
}

public function watermark($watermarkImage, $positionX = 0, $positionY = 0, $watermarkImageOpacity = 30, $repeat = false)
fballiano marked this conversation as resolved.
Show resolved Hide resolved
{
throw new RuntimeException('Watermark is not supported.');
}

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;

$this->_imageHandler->cropImage($newWidth, $newHeight, $left, $top);
$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 = $this->_imageHandler->getImageWidth();
$this->_imageSrcHeight = $this->_imageHandler->getImageHeight();
}

/**
* Gives real mime-type with not considering file type field
*
* @return string
*/
public function getMimeTypeWithOutFileType()
{
return $this->_fileMimeType;
}
}
Loading