Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AVIF in GD driver (requires PHP 8.1) #791

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Gd/Drawer.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private function getColor(ColorInterface $color)
throw new InvalidArgumentException('GD driver only supports RGB colors');
}

$gdColor = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), (100 - $color->getAlpha()) * 127 / 100);
$gdColor = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round((100 - $color->getAlpha()) * 127 / 100));
if ($gdColor === false) {
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()));
}
Expand Down
23 changes: 23 additions & 0 deletions src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,26 @@ private function saveOrOutput($format, array $options, $filename = null)
$args = array(&$this->resource, $filename);

switch ($format) {
case 'avif':
// ranges from 0 (worst quality, smaller file) to 100 (best quality, larger file). If -1 is provided, the default value is used
$quality = -1;
// ranges from 0 (slow, smaller file) to 10 (fast, larger file). If -1 is provided, the default value is used
$speed = -1;
if (!empty($options[$format . '_lossless'])) {
$quality = 100;
} else {
if (!isset($options[$format . '_quality'])) {
if (isset($options['quality'])) {
$options[$format . '_quality'] = $options['quality'];
}
}
if (isset($options[$format . '_quality'])) {
$quality = max(0, min(100, $options[$format . '_quality']));
}
}
$args[] = $quality;
$args[] = $speed;
break;
case 'bmp':
if (isset($options['compressed'])) {
$args[] = (bool) $options['compressed'];
Expand Down Expand Up @@ -812,6 +832,9 @@ private static function getSupportedFormats()
if (function_exists('imagewebp')) {
$supportedFormats['webp'] = array('mimeType' => 'image/webp');
}
if (function_exists('imageavif') && function_exists('imagecreatefromavif')) {
$supportedFormats['avif'] = array('mimeType' => 'image/avif');
}
ksort($supportedFormats);
}

Expand Down
5 changes: 4 additions & 1 deletion tests/tests/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ public function testSaveCompressionQuality($format, array $smallSizeOptions, arr
if ($format === 'webp' && !function_exists('imagewebp')) {
$this->markTestSkipped('GD webp support is not enabled');
}
if ($format === 'avif' || $format === 'heic' || $format === 'jxl') {
if ($format === 'avif' && !function_exists('imageavif')) {
$this->markTestSkipped('GD avif support is not enabled');
}
if ($format === 'heic' || $format === 'jxl') {
$this->markTestSkipped('GD does not support ' . strtoupper($format));
}

Expand Down
8 changes: 5 additions & 3 deletions tests/tests/Gd/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ public function testShouldOpenAWebPImage()
}

/**
* @group always-skipped
*
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAAvifImage()
*/
public function testShouldOpenAAvifImage()
{
$this->markTestSkipped('GD does not support AVIF');
if (!function_exists('imageavif')) {
$this->markTestSkipped('GD imageavif support is not enabled');
}

return parent::testShouldOpenAAvifImage();
}

/**
Expand Down