diff --git a/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php b/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php index 3f23902d..0799b92e 100644 --- a/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php +++ b/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php @@ -17,6 +17,13 @@ class NativeObjectDecoder extends SpecializableDecoder implements SpecializedInterface { + protected const SUPPORTED_COLORSPACES = [ + Imagick::COLORSPACE_SRGB, + Imagick::COLORSPACE_CMYK, + Imagick::COLORSPACE_HSL, + Imagick::COLORSPACE_HSB, + ]; + public function decode(mixed $input): ImageInterface|ColorInterface { if (!is_object($input)) { @@ -34,6 +41,13 @@ public function decode(mixed $input): ImageInterface|ColorInterface $input = $input->coalesceImages(); } + // turn image into rgb if colorspace if other than CMYK, RGB, HSL or HSV. + // this prevents working on greyscale colorspace images when loading + // from PNG color type greyscale format. + if (!in_array($input->getImageColorspace(), self::SUPPORTED_COLORSPACES)) { + $input->setImageColorspace(Imagick::COLORSPACE_SRGB); + } + // create image object $image = new Image( $this->driver(),