PHP 8.1: Leveraging AVIF Image Support in GD
In the continually evolving domain of image formats, AVIF (AV1 Image File) is a relatively new entrant that brings several attractive features to the table, including transparency and HDR. This new kid on the block is built on the AV1 video format, and impresses with its high compression abilities and lean file sizes, all while maintaining a royalty-free status.
Completing a round of standardization in the not-so-distant past, AVIF has quickly found acceptance with popular browsers like Chrome (from version 85) and Firefox (from version 86), both of which support AVIF images by default. Riding on the bandwagon of this rapid adoption, PHP 8.1 and its subsequent iterations now incorporate support for AVIF image format through its GD extension. This implies developers can now convert images between AVIF and other formats within the PHP environment, provided the GD extension has been compiled with AVIF support.
The Nitty-Gritty of Compiling GD with AVIF Support
To extend AVIF support, the GD extension calls upon the services of the libavif package, which provides the underlying AVIF codecs. However, to foster compatibility, the GD extension requires libavif version 0.8.2 or higher.
To get started with AVIF on Ubuntu/Debian, the libavif package can be installed using the apt install libavif-dev command. Similarly, for RHEL/Fedora, the equivalent command would be dnf install libavif-devel.
Once these dependencies are properly set, PHP can be compiled for AVIF support using the new --with-avif flag within the ./configure script.
Keep in mind that the given ./configure command represents a baseline, and production systems may need additional extensions enabled as per requirement.
On-Field Testing of AVIF Availability
Post-installation, you can go ahead with testing AVIF support. You can verify this through the phpinfo() function and the php -i CLI command. The output of the php -i | grep AVIF command should reveal if AVIF Support is enabled.
Navigating further down the road, PHP 8.1 introduces two new functions available exclusively when the GD extension is compiled with AVIF support - imagecreatefromavif and imageavif.
imagecreatefromavif & imageavif
Adhering to the naming conventions employed by other GD functions, imagecreatefromavif and imageavif pave the way for PHP 8.1 and higher to natively support AVIF image conversion.
Here's a quick rundown on what they offer:
- imagecreatefromavif function : This function accepts an AVIF image and returns a new GdImage instance which can subsequently be utilized for image manipulation or conversion to a different format. In case of failures, it returns 'false'.
- imageavif function: Relying on a GdImage instance as input, this function is responsible for outputting the AVIF formatted file. It offers flexibility in choosing the output destination - the binary data can be printed if $file parameter is null, or an AVIF image can be written to a filename if a string is provided. Additionally, it can write and close an image to a given file resource.
Both functions accommodate parameters for tuning the quality and speed of the conversion process to optimise for different use-cases.
To illustrate the functionality of these novel additions, let's traverse through a few scenarios:
To convert a JPEG image to AVIF
1$image = imagecreatefromjpeg('image.jpeg'); 2imageavif($image, 'image.avif'); 3
To convert an AVIF image to PNG
1$image = imagecreatefromavif('image.avif'); 2imagepng($image, 'image.png'); 3
To crop an image and save it in multiple formats
1$image = imagecreatefromjpeg('image.jpg'); 2$cropped_image = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => 200, 'height' => 200]); 3imagewebp($cropped_image, 'cropped.jpg'); 4imageavif($cropped_image, 'cropped.avif'); 5
Navigating Backward Compatibility Challenges
Although they offer robust functionality, remember that imagecreatefromavif and imageavif functions are exclusive to PHP 8.1+ and require GD extension to be built with AVIF support for functioning. Further, they warrant libavif version 0.8.2 or higher which is available in the default software repositories of recent Linux distributions (Ubuntu 21.04+, Debian 11+, Alpine 3.13+, Fedora 34 and up). Alternatively, the source can be cloned directly from AOMediaCodec/libavif repository.
For applications unable to use GD extension or mandating PHP 8.1, Image Magick PECL extension might serve as a worthy alternative. It also supports AVIF and encompasses wider availability.
Leaving legacy issues behind, these changes do not pose any significant backwards compatibility issues unless an application already declares its own imagecreatefromavif and imageavif functions. With this, it's time to welcome these new functions to the PHP family and make the most out of AVIF's capabilities.