33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Framework \Image \Adapter ;
78
89use Magento \Framework \Exception \LocalizedException ;
910
1011/**
11- * Image adapter from ImageMagick
12+ * Image adapter from ImageMagick.
1213 */
1314class 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}
0 commit comments