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.
13
+ *
14
+ * @property \Imagick $_imageHandler
12
15
*/
13
16
class ImageMagick extends AbstractAdapter
14
17
{
@@ -35,6 +38,19 @@ class ImageMagick extends AbstractAdapter
35
38
'sharpen ' => ['radius ' => 4 , 'deviation ' => 1 ],
36
39
];
37
40
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
+
38
54
/**
39
55
* Set/get background color. Check Imagick::COLOR_* constants
40
56
*
@@ -89,6 +105,8 @@ public function open($filename)
89
105
);
90
106
}
91
107
108
+ $ this ->getColorspace ();
109
+ $ this ->maybeConvertColorspace ();
92
110
$ this ->backgroundColor ();
93
111
$ this ->getMimeType ();
94
112
}
@@ -136,8 +154,8 @@ protected function _applyOptions()
136
154
/**
137
155
* Render image and return its binary contents
138
156
*
139
- * @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
140
157
* @return string
158
+ * @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
141
159
*/
142
160
public function getImage ()
143
161
{
@@ -265,7 +283,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
265
283
$ this ->_checkCanProcess ();
266
284
267
285
$ opacity = $ this ->getWatermarkImageOpacity () ? $ this ->getWatermarkImageOpacity () : $ opacity ;
268
- $ opacity = (double )number_format ($ opacity / 100 , 1 );
286
+ $ opacity = (double ) number_format ($ opacity / 100 , 1 );
269
287
270
288
$ watermark = new \Imagick ($ imagePath );
271
289
@@ -395,8 +413,8 @@ public function getColorAt($x, $y)
395
413
/**
396
414
* Check whether the adapter can work with the image
397
415
*
398
- * @throws \LogicException
399
416
* @return true
417
+ * @throws \LogicException
400
418
*/
401
419
protected function _checkCanProcess ()
402
420
{
@@ -562,4 +580,42 @@ private function addSingleWatermark($positionX, int $positionY, \Imagick $waterm
562
580
$ compositeChannels
563
581
);
564
582
}
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
+ }
565
621
}
0 commit comments