PHP 8.1: GD: AVIF image support

Version8.1
TypeNew Feature

AVIF (AV1 Image File) is a relatively new image format that supports various features such as transparency and HDR. It is a royalty-free image format based on the AV1 video format, and provides higher compression with lower file sizes.

AVIF image format was standardized recently, and browsers such as Chrome 85+ and Firefox 86+ already support AVIF images by default.

The GD extension in PHP 8.1 and later supports AVIF image format, making it possible to convert images from and to AVIF. Note that the GD extension must be compiled with AVIF support. It depends on libavif package, which may not be available in default repositories of older operating system versions.

Compile GD with AVIF Support

GD extension depends on the libavif package to provide the underlying AVIF codecs. GD extension requires libavif version 0.8.2 or higher.

Ubuntu/Debian

apt install libavif-dev

RHEL/Fedora

dnf install libavif-devel

Once the dependencies are installed, PHP can be compiled with AVIF support with the new --with-avif flag in the ./configure script.

./buildconf --force
./configure --enable-gd --with-avif

Note that the ./configure command above is just an example; For production systems, make sure to enable other extensions as well.

Testing AVIF Availability

phpinfo() and php -i CLI command should reveal AVIF image.

php -i | grep AVIF
AVIF Support => enabled

A list of GD features can also be retrieved from the PHP gd_info function.

gd_info();
[
     "GD Version" => "bundled (2.1.0 compatible)",
     "FreeType Support" => false,
     // ...
     "AVIF Support" => true,
]

In addition, there are two new functions, imagecreatefromavif and imageavif that are only available if the GD extension is compiled with AVIF support.

if (function_exists('imageavif')) {
    // AVIF support available.
}

New AVIF Functions

GD extension in PHP 8.1 adds two new functions if the extension is compiled with AVIF support. They follow the same convention of other GD functions, such as imagecreatefromjpeg/imagejpeg, imagecreatefrompng/imagepng, etc.

imagecreatefromavif function

function imagecreatefromavif(string $filename): GdImage|false {}

imagecreatefromavif function returns a new GdImage instance from a given AVIF image. This GdImage instance can then be used to manipulate/convert the image.

imagecreatefromavif function is similar to the existing functions such as imagecreatefromjpeg and imagecreatefrompng that they all return a GdImage, or false on failures.

imageavif function

/**
 * @param resource|string|null $file
 */
function imageavif(GdImage $image, $file = null, int $quality = -1, int $speed = -1): bool {}

imageavif function outputs the AVIF formatted file.

If the $file parameter is null, imageavif function prints the binary data. If a file name is provided (string), the AVIF image will be written to that file name, or if a file resource is given, the image will be written and closed to that resource.

The int $quality and int $speed optional parameters are used to configure the quality and speed values.

  • int $quality: The default value of -1 reuses libavif default value. The accepted values are 0 (worst quality) through 100 (highest quality). Any integers out of this range are clamped to the 0-100 range..
  • int $speed: Default value of -1 reuses the libavif default value, which is equivalent to 6. Accepted values are int the range of 0 (slowest) through 10 (fastest). Integers outside the 0-10 range are clamped.

Default values provide balanced speed and quality. Try to use the default $speed and $quality values unless there is a specific reason.

Usage Examples

Convert and save JPEG image to AVIF

$image = imagecreatefromjpeg('image.jpeg');
imageavif($image, 'image.avif');

Convert and save AVIF image to PNG

$image = imagecreatefromavif('image.avif');
imagepng($image, 'image.png');

Crop and save image in multiple formats

$image = imagecreatefromjpeg('image.jpg');

$cropped_image = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => 200, 'height' => 200]);

imagewebp($cropped_image, 'cropped.jpg');
imageavif($cropped_image, 'cropped.avif');

Backwards Compatibility Impact

The new imagecreatefromavif and imageavif functions are only available on PHP 8.1+, and if the GD extension is built with AVIF support. It requires libavif version 0.8.2 or higher.

Ubuntu 21.04 and up, Debian 11 and up, Alpine 3.13 and up, Fedora 34 are some of the Linux distributions that provide libavif-dev/libavif-devel packages in their default software repositories.

Alternately, the source can be directly cloned from AOMediaCodec/libavif repository.


On applications that cannot use GD extension or require PHP 8.1, Image Magick PECL extension, which also supports AVIF, might be a more viable option with wider availability.

$imagick = new Imagick();
$imagick->readImage('image.jpg');
$imagick->setImageFormat('avif');
$imagick->writeImage('image.avif');

Unless an application declares their own imagecreatefromavif and imageavif functions, this change should not cause any backwards compatibility issues.


Implementation