1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-23 22:12:51 +02:00

Merge pull request #1113 from gijsdev/gd-avif

GD AVIF support
This commit is contained in:
Oliver Vogel
2021-10-03 16:17:12 +02:00
committed by GitHub
2 changed files with 28 additions and 13 deletions

View File

@@ -147,11 +147,25 @@ class Encoder extends \Intervention\Image\AbstractEncoder
*/ */
protected function processAvif() protected function processAvif()
{ {
if ( ! function_exists('imageavif')) {
throw new NotSupportedException( throw new NotSupportedException(
"AVIF format is not supported by Gd Driver." "AVIF format is not supported by PHP installation."
); );
} }
ob_start();
$resource = $this->image->getCore();
imagepalettetotruecolor($resource);
imagealphablending($resource, true);
imagesavealpha($resource, true);
imageavif($resource, null, $this->quality);
$this->image->mime = defined('IMAGETYPE_AVIF') ? image_type_to_mime_type(IMAGETYPE_AVIF) : 'image/avif';
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
/** /**
* Processes and returns encoded image as HEIC string * Processes and returns encoded image as HEIC string
* *

View File

@@ -75,17 +75,18 @@ class EncoderTest extends TestCase
} }
} }
/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
public function testProcessAvifGd() public function testProcessAvifGd()
{ {
if (function_exists('imageavif')) {
$core = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); $core = imagecreatefromjpeg(__DIR__.'/images/test.jpg');
$encoder = new GdEncoder; $encoder = new GdEncoder;
$image = Mockery::mock('\Intervention\Image\Image'); $image = Mockery::mock('\Intervention\Image\Image');
$image->shouldReceive('getCore')->once()->andReturn($core);
$image->shouldReceive('setEncoded')->once()->andReturn($image);
$img = $encoder->process($image, 'avif', 90); $img = $encoder->process($image, 'avif', 90);
$this->assertInstanceOf('Intervention\Image\Image', $img); $this->assertInstanceOf('Intervention\Image\Image', $img);
$this->assertEquals('image/avif; charset=binary', $this->getMime($encoder->result));
}
} }
/** /**