Skip to content

Commit 615c89c

Browse files
ENGCOM-8296: Add Imagick CMYK to SRGB conversion #28809
- Merge Pull Request #28809 from tdgroot/magento2:cmyk_to_rgb_conversion - Merged commits: 1. 5fd9733 2. 4d801c5 3. 5015928 4. 2cc29b7 5. 16962dd 6. a4bfb72 7. 82565e8 8. 9efd600 9. 8b251e4 10. efb7354 11. 4cc567d
2 parents 29bf891 + 4cc567d commit 615c89c

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\Image\Adapter;
78

89
use Magento\Framework\Exception\LocalizedException;
910

1011
/**
11-
* Image adapter from ImageMagick
12+
* Image adapter from ImageMagick.
1213
*/
1314
class ImageMagick extends AbstractAdapter
1415
{
@@ -35,6 +36,18 @@ class ImageMagick extends AbstractAdapter
3536
'sharpen' => ['radius' => 4, 'deviation' => 1],
3637
];
3738

39+
/**
40+
* @var \Imagick
41+
*/
42+
protected $_imageHandler;
43+
44+
/**
45+
* Colorspace of the image
46+
*
47+
* @var int
48+
*/
49+
private $colorspace = -1;
50+
3851
/**
3952
* Set/get background color. Check Imagick::COLOR_* constants
4053
*
@@ -94,6 +107,8 @@ public function open($filename)
94107
);
95108
}
96109

110+
$this->getColorspace();
111+
$this->maybeConvertColorspace();
97112
$this->backgroundColor();
98113
$this->getMimeType();
99114
}
@@ -158,8 +173,8 @@ protected function _applyOptions()
158173
/**
159174
* Render image and return its binary contents
160175
*
161-
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
162176
* @return string
177+
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
163178
*/
164179
public function getImage()
165180
{
@@ -288,7 +303,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
288303
$this->_checkCanProcess();
289304

290305
$opacity = $this->getWatermarkImageOpacity() ? $this->getWatermarkImageOpacity() : $opacity;
291-
$opacity = (double)number_format($opacity / 100, 1);
306+
$opacity = (double) number_format($opacity / 100, 1);
292307

293308
$watermark = new \Imagick($imagePath);
294309

@@ -419,8 +434,8 @@ public function getColorAt($x, $y)
419434
/**
420435
* Check whether the adapter can work with the image
421436
*
422-
* @throws \LogicException
423437
* @return true
438+
* @throws \LogicException
424439
*/
425440
protected function _checkCanProcess()
426441
{
@@ -586,4 +601,31 @@ private function addSingleWatermark($positionX, int $positionY, \Imagick $waterm
586601
$compositeChannels
587602
);
588603
}
604+
605+
/**
606+
* Get and store the image colorspace.
607+
*
608+
* @return int
609+
*/
610+
private function getColorspace(): int
611+
{
612+
if ($this->colorspace === -1) {
613+
$this->colorspace = $this->_imageHandler->getImageColorspace();
614+
}
615+
616+
return $this->colorspace;
617+
}
618+
619+
/**
620+
* Convert colorspace to SRGB if current colorspace is COLORSPACE_CMYK or COLORSPACE_UNDEFINED.
621+
*
622+
* @return void
623+
*/
624+
private function maybeConvertColorspace(): void
625+
{
626+
if ($this->colorspace === \Imagick::COLORSPACE_CMYK || $this->colorspace === \Imagick::COLORSPACE_UNDEFINED) {
627+
$this->_imageHandler->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
628+
$this->colorspace = $this->_imageHandler->getImageColorspace();
629+
}
630+
}
589631
}

lib/internal/Magento/Framework/Image/Test/Unit/Adapter/ImageMagickTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testWatermark($imagePath, $expectedMessage)
7676
/**
7777
* @return array
7878
*/
79-
public function watermarkDataProvider()
79+
public function watermarkDataProvider(): array
8080
{
8181
return [
8282
['', ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],

0 commit comments

Comments
 (0)