diff --git a/src/Intervention/Image/AbstractEncoder.php b/src/Intervention/Image/AbstractEncoder.php index 7c2e3590..77544653 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 * @@ -182,6 +189,11 @@ abstract class AbstractEncoder case 'image/avif': $this->result = $this->processAvif(); break; + + case 'heic': + case 'image/heic': + $this->result = $this->processHeic(); + break; default: throw new NotSupportedException( diff --git a/src/Intervention/Image/Gd/Encoder.php b/src/Intervention/Image/Gd/Encoder.php index ab6d1f05..00c4b69a 100644 --- a/src/Intervention/Image/Gd/Encoder.php +++ b/src/Intervention/Image/Gd/Encoder.php @@ -57,6 +57,11 @@ class Encoder extends \Intervention\Image\AbstractEncoder return $buffer; } + /** + * Processes and returns encoded image as WEBP string + * + * @return string + */ protected function processWebp() { if ( ! function_exists('imagewebp')) { @@ -146,4 +151,16 @@ class Encoder extends \Intervention\Image\AbstractEncoder "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..feb8a9d5 100644 --- a/src/Intervention/Image/Imagick/Encoder.php +++ b/src/Intervention/Image/Imagick/Encoder.php @@ -183,6 +183,11 @@ class Encoder extends AbstractEncoder return $imagick->getImagesBlob(); } + /** + * Processes and returns encoded image as AVIF string + * + * @return string + */ protected function processAvif() { if ( ! \Imagick::queryFormats('AVIF')) { @@ -204,4 +209,31 @@ class Encoder extends AbstractEncoder return $imagick->getImagesBlob(); } + + /** + * Processes and returns encoded image as HEIC string + * + * @return string + */ + 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');