Skip to content

Commit 728d653

Browse files
authored
Merge pull request #791 from mlocati/gd-avif
Support AVIF in GD driver (requires PHP 8.1)
2 parents 9f51674 + 9409fa7 commit 728d653

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/Gd/Drawer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private function getColor(ColorInterface $color)
446446
throw new InvalidArgumentException('GD driver only supports RGB colors');
447447
}
448448

449-
$gdColor = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), (100 - $color->getAlpha()) * 127 / 100);
449+
$gdColor = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round((100 - $color->getAlpha()) * 127 / 100));
450450
if ($gdColor === false) {
451451
throw new RuntimeException(sprintf('Unable to allocate color "RGB(%s, %s, %s)" with transparency of %d percent', $color->getRed(), $color->getGreen(), $color->getBlue(), $color->getAlpha()));
452452
}

src/Gd/Image.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,26 @@ private function saveOrOutput($format, array $options, $filename = null)
608608
$args = array(&$this->resource, $filename);
609609

610610
switch ($format) {
611+
case 'avif':
612+
// ranges from 0 (worst quality, smaller file) to 100 (best quality, larger file). If -1 is provided, the default value is used
613+
$quality = -1;
614+
// ranges from 0 (slow, smaller file) to 10 (fast, larger file). If -1 is provided, the default value is used
615+
$speed = -1;
616+
if (!empty($options[$format . '_lossless'])) {
617+
$quality = 100;
618+
} else {
619+
if (!isset($options[$format . '_quality'])) {
620+
if (isset($options['quality'])) {
621+
$options[$format . '_quality'] = $options['quality'];
622+
}
623+
}
624+
if (isset($options[$format . '_quality'])) {
625+
$quality = max(0, min(100, $options[$format . '_quality']));
626+
}
627+
}
628+
$args[] = $quality;
629+
$args[] = $speed;
630+
break;
611631
case 'bmp':
612632
if (isset($options['compressed'])) {
613633
$args[] = (bool) $options['compressed'];
@@ -812,6 +832,9 @@ private static function getSupportedFormats()
812832
if (function_exists('imagewebp')) {
813833
$supportedFormats['webp'] = array('mimeType' => 'image/webp');
814834
}
835+
if (function_exists('imageavif') && function_exists('imagecreatefromavif')) {
836+
$supportedFormats['avif'] = array('mimeType' => 'image/avif');
837+
}
815838
ksort($supportedFormats);
816839
}
817840

tests/tests/Gd/ImageTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ public function testSaveCompressionQuality($format, array $smallSizeOptions, arr
193193
if ($format === 'webp' && !function_exists('imagewebp')) {
194194
$this->markTestSkipped('GD webp support is not enabled');
195195
}
196-
if ($format === 'avif' || $format === 'heic' || $format === 'jxl') {
196+
if ($format === 'avif' && !function_exists('imageavif')) {
197+
$this->markTestSkipped('GD avif support is not enabled');
198+
}
199+
if ($format === 'heic' || $format === 'jxl') {
197200
$this->markTestSkipped('GD does not support ' . strtoupper($format));
198201
}
199202

tests/tests/Gd/ImagineTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ public function testShouldOpenAWebPImage()
4848
}
4949

5050
/**
51-
* @group always-skipped
52-
*
5351
* {@inheritdoc}
5452
*
5553
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAAvifImage()
5654
*/
5755
public function testShouldOpenAAvifImage()
5856
{
59-
$this->markTestSkipped('GD does not support AVIF');
57+
if (!function_exists('imageavif')) {
58+
$this->markTestSkipped('GD imageavif support is not enabled');
59+
}
60+
61+
return parent::testShouldOpenAAvifImage();
6062
}
6163

6264
/**

0 commit comments

Comments
 (0)