Skip to content

Add Imagick CMYK to SRGB conversion #28809

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 46 additions & 4 deletions lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Image\Adapter;

use Magento\Framework\Exception\LocalizedException;

/**
* Image adapter from ImageMagick
* Image adapter from ImageMagick.
*/
class ImageMagick extends AbstractAdapter
{
Expand All @@ -35,6 +36,18 @@ class ImageMagick extends AbstractAdapter
'sharpen' => ['radius' => 4, 'deviation' => 1],
];

/**
* @var \Imagick
*/
protected $_imageHandler;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you need this var? It should be inherited from the parent class

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It provides type information for within the ImageMagick class.


/**
* Colorspace of the image
*
* @var int
*/
private $colorspace = -1;

/**
* Set/get background color. Check Imagick::COLOR_* constants
*
Expand Down Expand Up @@ -94,6 +107,8 @@ public function open($filename)
);
}

$this->getColorspace();
$this->maybeConvertColorspace();
$this->backgroundColor();
$this->getMimeType();
}
Expand Down Expand Up @@ -158,8 +173,8 @@ protected function _applyOptions()
/**
* Render image and return its binary contents
*
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
* @return string
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
*/
public function getImage()
{
Expand Down Expand Up @@ -288,7 +303,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
$this->_checkCanProcess();

$opacity = $this->getWatermarkImageOpacity() ? $this->getWatermarkImageOpacity() : $opacity;
$opacity = (double)number_format($opacity / 100, 1);
$opacity = (double) number_format($opacity / 100, 1);

$watermark = new \Imagick($imagePath);

Expand Down Expand Up @@ -419,8 +434,8 @@ public function getColorAt($x, $y)
/**
* Check whether the adapter can work with the image
*
* @throws \LogicException
* @return true
* @throws \LogicException
*/
protected function _checkCanProcess()
{
Expand Down Expand Up @@ -586,4 +601,31 @@ private function addSingleWatermark($positionX, int $positionY, \Imagick $waterm
$compositeChannels
);
}

/**
* Get and store the image colorspace.
*
* @return int
*/
private function getColorspace(): int
{
if ($this->colorspace === -1) {
$this->colorspace = $this->_imageHandler->getImageColorspace();
}

return $this->colorspace;
}

/**
* Convert colorspace to SRGB if current colorspace is COLORSPACE_CMYK or COLORSPACE_UNDEFINED.
*
* @return void
*/
private function maybeConvertColorspace(): void
{
if ($this->colorspace === \Imagick::COLORSPACE_CMYK || $this->colorspace === \Imagick::COLORSPACE_UNDEFINED) {
$this->_imageHandler->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
$this->colorspace = $this->_imageHandler->getImageColorspace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testWatermark($imagePath, $expectedMessage)
/**
* @return array
*/
public function watermarkDataProvider()
public function watermarkDataProvider(): array
{
return [
['', ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],
Expand Down