From e9ecab2a549db5d943244114c37b222a6e589952 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 21 Dec 2023 21:25:17 +0100 Subject: [PATCH] Implement Jpeg2000 encoding with Imagick driver --- .../Imagick/Encoders/Jpeg2000Encoder.php | 33 +++++++++++++++++++ src/Encoders/Jpeg2000Encoder.php | 10 ++++++ src/Image.php | 22 +++++++++++++ src/Interfaces/ImageInterface.php | 8 +++++ 4 files changed, 73 insertions(+) create mode 100644 src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php create mode 100644 src/Encoders/Jpeg2000Encoder.php diff --git a/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php b/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php new file mode 100644 index 00000000..1ff0a95a --- /dev/null +++ b/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php @@ -0,0 +1,33 @@ +core()->native(); + $imagick->setImageBackgroundColor('white'); + $imagick->setBackgroundColor('white'); + $imagick->setFormat($format); + $imagick->setImageFormat($format); + $imagick->setCompression($compression); + $imagick->setImageCompression($compression); + $imagick->setCompressionQuality($this->quality); + $imagick->setImageCompressionQuality($this->quality); + + return new EncodedImage($imagick->getImagesBlob(), 'image/jp2'); + } +} diff --git a/src/Encoders/Jpeg2000Encoder.php b/src/Encoders/Jpeg2000Encoder.php new file mode 100644 index 00000000..0eb60a54 --- /dev/null +++ b/src/Encoders/Jpeg2000Encoder.php @@ -0,0 +1,10 @@ +toJpeg($quality); } + /** + * {@inheritdoc} + * + * @see ImageInterface::toJpeg() + */ + public function toJpeg2000(int $quality = 75): EncodedImageInterface + { + return $this->encode(new Jpeg2000Encoder($quality)); + } + + /** + * ALias of self::toJpeg2000() + * + * @param int $quality + * @return EncodedImageInterface + */ + public function toJp2(int $quality = 75): EncodedImageInterface + { + return $this->toJpeg2000($quality); + } + /** * {@inheritdoc} * diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index bd5affda..af7383f0 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -603,6 +603,14 @@ interface ImageInterface extends IteratorAggregate, Countable */ public function toJpeg(int $quality = 75): EncodedImageInterface; + /** + * Encode image to Jpeg2000 format + * + * @param int $quality + * @return EncodedImageInterface + */ + public function toJpeg2000(int $quality = 75): EncodedImageInterface; + /** * Encode image to Webp format *