From 3ddcc2d55dbe4b1a4cb93a5cefc62582eb1665a8 Mon Sep 17 00:00:00 2001 From: Michiel Vugteveen Date: Fri, 23 Jul 2021 20:45:18 +0200 Subject: [PATCH] Add HEIC support --- src/Intervention/Image/AbstractEncoder.php | 7 +++++++ src/Intervention/Image/Gd/Encoder.php | 11 +++++++++++ src/Intervention/Image/Imagick/Encoder.php | 22 ++++++++++++++++++++++ tests/EncoderTest.php | 22 ++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/src/Intervention/Image/AbstractEncoder.php b/src/Intervention/Image/AbstractEncoder.php index 7c2e3590..dab783ad 100644 --- a/src/Intervention/Image/AbstractEncoder.php +++ b/src/Intervention/Image/AbstractEncoder.php @@ -91,6 +91,13 @@ abstract class AbstractEncoder */ abstract protected function processAvif(); + /** + * Processes and returns image as Heic encoded string + * + * @return string + */ + abstract protected function processHeic(); + /** * Process a given image * diff --git a/src/Intervention/Image/Gd/Encoder.php b/src/Intervention/Image/Gd/Encoder.php index ab6d1f05..b196de16 100644 --- a/src/Intervention/Image/Gd/Encoder.php +++ b/src/Intervention/Image/Gd/Encoder.php @@ -144,6 +144,17 @@ class Encoder extends \Intervention\Image\AbstractEncoder { throw new NotSupportedException( "AVIF format is not supported by Gd Driver." + + + /** + * Processes and returns encoded image as HEIC string + * + * @return string + */ + protected function processHeic() + { + throw new NotSupportedException( + "HEIC format is not supported by Gd Driver." ); } } diff --git a/src/Intervention/Image/Imagick/Encoder.php b/src/Intervention/Image/Imagick/Encoder.php index df7b5b82..922fb265 100644 --- a/src/Intervention/Image/Imagick/Encoder.php +++ b/src/Intervention/Image/Imagick/Encoder.php @@ -204,4 +204,26 @@ class Encoder extends AbstractEncoder return $imagick->getImagesBlob(); } + + protected function processHeic() + { + if ( ! \Imagick::queryFormats('HEIC')) { + throw new NotSupportedException( + "HEIC format is not supported by Imagick installation." + ); + } + + $format = 'heic'; + $compression = \Imagick::COMPRESSION_UNDEFINED; + + $imagick = $this->image->getCore(); + $imagick->setFormat($format); + $imagick->setImageFormat($format); + $imagick->setCompression($compression); + $imagick->setImageCompression($compression); + $imagick->setCompressionQuality($this->quality); + $imagick->setImageCompressionQuality($this->quality); + + return $imagick->getImagesBlob(); + } } diff --git a/tests/EncoderTest.php b/tests/EncoderTest.php index a8573273..f04b50a4 100644 --- a/tests/EncoderTest.php +++ b/tests/EncoderTest.php @@ -88,6 +88,18 @@ class EncoderTest extends TestCase $this->assertInstanceOf('Intervention\Image\Image', $img); } + /** + * @expectedException \Intervention\Image\Exception\NotSupportedException + */ + public function testProcessHeicGd() + { + $core = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); + $encoder = new GdEncoder; + $image = Mockery::mock('\Intervention\Image\Image'); + $img = $encoder->process($image, 'heic', 90); + $this->assertInstanceOf('Intervention\Image\Image', $img); + } + /** * @expectedException \Intervention\Image\Exception\NotSupportedException */ @@ -219,6 +231,16 @@ class EncoderTest extends TestCase $img = $encoder->process($image, 'avif', 90); } + /** + * @expectedException \Intervention\Image\Exception\NotSupportedException + */ + public function testProcessHeicImagick() + { + $encoder = new ImagickEncoder; + $image = Mockery::mock('\Intervention\Image\Image'); + $img = $encoder->process($image, 'heic', 90); + } + public function testProcessTiffImagick() { $core = $this->getImagickMock('tiff');