Skip to content

Commit 5fd9733

Browse files
author
Timon de Groot
committed
Add Imagick CMYK to SRGB conversion
1 parent c284664 commit 5fd9733

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

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

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
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.
13+
*
14+
* @property \Imagick $_imageHandler
1215
*/
1316
class ImageMagick extends AbstractAdapter
1417
{
@@ -35,6 +38,19 @@ class ImageMagick extends AbstractAdapter
3538
'sharpen' => ['radius' => 4, 'deviation' => 1],
3639
];
3740

41+
/**
42+
* Colorspace of the image
43+
*
44+
* @var int
45+
*/
46+
protected $colorspace = -1;
47+
/**
48+
* Original colorspace of the image
49+
*
50+
* @var int
51+
*/
52+
protected $originalColorspace = -1;
53+
3854
/**
3955
* Set/get background color. Check Imagick::COLOR_* constants
4056
*
@@ -89,6 +105,8 @@ public function open($filename)
89105
);
90106
}
91107

108+
$this->getColorspace();
109+
$this->maybeConvertColorspace();
92110
$this->backgroundColor();
93111
$this->getMimeType();
94112
}
@@ -136,8 +154,8 @@ protected function _applyOptions()
136154
/**
137155
* Render image and return its binary contents
138156
*
139-
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
140157
* @return string
158+
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
141159
*/
142160
public function getImage()
143161
{
@@ -265,7 +283,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
265283
$this->_checkCanProcess();
266284

267285
$opacity = $this->getWatermarkImageOpacity() ? $this->getWatermarkImageOpacity() : $opacity;
268-
$opacity = (double)number_format($opacity / 100, 1);
286+
$opacity = (double) number_format($opacity / 100, 1);
269287

270288
$watermark = new \Imagick($imagePath);
271289

@@ -395,8 +413,8 @@ public function getColorAt($x, $y)
395413
/**
396414
* Check whether the adapter can work with the image
397415
*
398-
* @throws \LogicException
399416
* @return true
417+
* @throws \LogicException
400418
*/
401419
protected function _checkCanProcess()
402420
{
@@ -562,4 +580,42 @@ private function addSingleWatermark($positionX, int $positionY, \Imagick $waterm
562580
$compositeChannels
563581
);
564582
}
583+
584+
/**
585+
* Get and store the image colorspace.
586+
*
587+
* @return int
588+
*/
589+
public function getColorspace(): int
590+
{
591+
if ($this->colorspace === -1) {
592+
$this->originalColorspace = $this->colorspace = $this->_imageHandler->getImageColorspace();
593+
}
594+
595+
return $this->colorspace;
596+
}
597+
598+
/**
599+
* Get the original image colorspace.
600+
*
601+
* @return int
602+
*/
603+
public function getOriginalColorspace(): int
604+
{
605+
return $this->originalColorspace;
606+
}
607+
608+
/**
609+
* Convert colorspace to SRGB if current colorspace
610+
* is COLORSPACE_CMYK or COLORSPACE_UNDEFINED.
611+
*
612+
* @return void
613+
*/
614+
private function maybeConvertColorspace(): void
615+
{
616+
if ($this->colorspace === \Imagick::COLORSPACE_CMYK || $this->colorspace === \Imagick::COLORSPACE_UNDEFINED) {
617+
$this->_imageHandler->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
618+
$this->colorspace = \Imagick::COLORSPACE_SRGB;
619+
}
620+
}
565621
}

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public function testWatermark($imagePath, $expectedMessage)
7373
$this->imageMagic->watermark($imagePath);
7474
}
7575

76-
/**
77-
* @return array
78-
*/
79-
public function watermarkDataProvider()
76+
public function watermarkDataProvider(): array
8077
{
8178
return [
8279
['', ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],
@@ -88,6 +85,44 @@ public function watermarkDataProvider()
8885
];
8986
}
9087

88+
/**
89+
* @param string $imagePath
90+
* @throws \Magento\Framework\Exception\LocalizedException
91+
* @dataProvider cmykDataProvider
92+
*/
93+
public function testCmyk(string $imagePath)
94+
{
95+
$this->imageMagic->open($imagePath);
96+
$this->assertEquals(\Imagick::COLORSPACE_CMYK, $this->imageMagic->getOriginalColorspace());
97+
$this->assertEquals(\Imagick::COLORSPACE_SRGB, $this->imageMagic->getColorspace());
98+
}
99+
100+
public function cmykDataProvider(): array
101+
{
102+
return [
103+
[__DIR__ . '/_files/cmyk_image.jpg']
104+
];
105+
}
106+
107+
/**
108+
* @param string $imagePath
109+
* @throws \Magento\Framework\Exception\LocalizedException
110+
* @dataProvider srgbDataProvider
111+
*/
112+
public function testSrgb(string $imagePath)
113+
{
114+
$this->imageMagic->open($imagePath);
115+
$this->assertEquals(\Imagick::COLORSPACE_SRGB, $this->imageMagic->getOriginalColorspace());
116+
$this->assertEquals(\Imagick::COLORSPACE_SRGB, $this->imageMagic->getColorspace());
117+
}
118+
119+
public function srgbDataProvider(): array
120+
{
121+
return [
122+
[__DIR__ . '/_files/srgb_image.jpg']
123+
];
124+
}
125+
91126
public function testSaveWithException()
92127
{
93128
$this->expectException('Exception');
Loading
Loading

0 commit comments

Comments
 (0)