3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Framework \Image \Adapter ;
7
8
8
9
use Magento \Framework \Exception \LocalizedException ;
9
10
10
11
/**
11
- * Image adapter from ImageMagick
12
+ * Image adapter from ImageMagick.
12
13
*/
13
14
class ImageMagick extends AbstractAdapter
14
15
{
@@ -35,6 +36,18 @@ class ImageMagick extends AbstractAdapter
35
36
'sharpen ' => ['radius ' => 4 , 'deviation ' => 1 ],
36
37
];
37
38
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
+
38
51
/**
39
52
* Set/get background color. Check Imagick::COLOR_* constants
40
53
*
@@ -94,6 +107,8 @@ public function open($filename)
94
107
);
95
108
}
96
109
110
+ $ this ->getColorspace ();
111
+ $ this ->maybeConvertColorspace ();
97
112
$ this ->backgroundColor ();
98
113
$ this ->getMimeType ();
99
114
}
@@ -158,8 +173,8 @@ protected function _applyOptions()
158
173
/**
159
174
* Render image and return its binary contents
160
175
*
161
- * @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
162
176
* @return string
177
+ * @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
163
178
*/
164
179
public function getImage ()
165
180
{
@@ -288,7 +303,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
288
303
$ this ->_checkCanProcess ();
289
304
290
305
$ opacity = $ this ->getWatermarkImageOpacity () ? $ this ->getWatermarkImageOpacity () : $ opacity ;
291
- $ opacity = (double )number_format ($ opacity / 100 , 1 );
306
+ $ opacity = (double ) number_format ($ opacity / 100 , 1 );
292
307
293
308
$ watermark = new \Imagick ($ imagePath );
294
309
@@ -419,8 +434,8 @@ public function getColorAt($x, $y)
419
434
/**
420
435
* Check whether the adapter can work with the image
421
436
*
422
- * @throws \LogicException
423
437
* @return true
438
+ * @throws \LogicException
424
439
*/
425
440
protected function _checkCanProcess ()
426
441
{
@@ -586,4 +601,31 @@ private function addSingleWatermark($positionX, int $positionY, \Imagick $waterm
586
601
$ compositeChannels
587
602
);
588
603
}
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
+ }
589
631
}
0 commit comments