diff --git a/src/Intervention/Image/AbstractEncoder.php b/src/Intervention/Image/AbstractEncoder.php index d3e59eaa4..f88c55e78 100644 --- a/src/Intervention/Image/AbstractEncoder.php +++ b/src/Intervention/Image/AbstractEncoder.php @@ -84,6 +84,13 @@ abstract protected function processIco(); */ abstract protected function processWebp(); + /** + * Processes and returns image as Avif encoded string + * + * @return string + */ + abstract protected function processAvif(); + /** * Process a given image * @@ -168,6 +175,11 @@ public function process(Image $image, $format = null, $quality = null) case 'image/x-webp': $this->result = $this->processWebp(); break; + + case 'avif': + case 'image/avif': + $this->result = $this->processAvif(); + break; default: throw new NotSupportedException( diff --git a/src/Intervention/Image/Gd/Encoder.php b/src/Intervention/Image/Gd/Encoder.php index d8cc02178..f289a6467 100644 --- a/src/Intervention/Image/Gd/Encoder.php +++ b/src/Intervention/Image/Gd/Encoder.php @@ -121,4 +121,16 @@ protected function processPsd() "PSD format is not supported by Gd Driver." ); } + + /** + * Processes and returns encoded image as AVIF string + * + * @return string + */ + protected function processAvif() + { + throw new NotSupportedException( + "AVIF format is not supported by Gd Driver." + ); + } } diff --git a/src/Intervention/Image/Imagick/Encoder.php b/src/Intervention/Image/Imagick/Encoder.php index ac7345e4a..956e5fdd6 100644 --- a/src/Intervention/Image/Imagick/Encoder.php +++ b/src/Intervention/Image/Imagick/Encoder.php @@ -170,4 +170,26 @@ protected function processPsd() return $imagick->getImagesBlob(); } + + protected function processAvif() + { + if ( ! \Imagick::queryFormats('AVIF')) { + throw new NotSupportedException( + "AVIF format is not supported by Imagick installation." + ); + } + + $format = 'avif'; + $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 834618c3a..3a18e14a8 100644 --- a/tests/EncoderTest.php +++ b/tests/EncoderTest.php @@ -61,6 +61,18 @@ public function testProcessWebpGd() } } + /** + * @expectedException \Intervention\Image\Exception\NotSupportedException + */ + public function testProcessAvifGd() + { + $core = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); + $encoder = new GdEncoder; + $image = Mockery::mock('\Intervention\Image\Image'); + $img = $encoder->process($image, 'avif', 90); + $this->assertInstanceOf('Intervention\Image\Image', $img); + } + /** * @expectedException \Intervention\Image\Exception\NotSupportedException */ @@ -180,6 +192,16 @@ public function testProcessWebpImagick() $img = $encoder->process($image, 'webp', 90); } + /** + * @expectedException \Intervention\Image\Exception\NotSupportedException + */ + public function testProcessAvifImagick() + { + $encoder = new ImagickEncoder; + $image = Mockery::mock('\Intervention\Image\Image'); + $img = $encoder->process($image, 'avif', 90); + } + public function testProcessTiffImagick() { $core = $this->getImagickMock('tiff');