All posts
Published in PHP

PHP 8.1: GD: AVIF image support

Profile image of Atakan Demircioğlu
By Atakan Demircioğlu
Fullstack Developer

With the latest PHP 8.1 update bringing a host of improvements and new features, PHP web development has never been more exciting. One notable feature is the introduction of the GD: AVIF image support. Let's get into the details.

What is GD: Avif Image Support?

GD is a powerful open-source code library used for creating and manipulating images dynamically. AVIF, on the other hand, is a new image format based on the modern AV1 video codec. It offers superior compression and efficiency over older image formats, without compromising on quality. In PHP 8.1, GD: AVIF Image Support refers to the addition of functions to handle AVIF images using the GD library.

Advantages of AVIF Images

AVIF holds a lot of promises and benefits for web developers and users. This new image format offers better compression than legacy formats like JPEG and PNG. This means that AVIF images use less storage and bandwidth, resulting in faster loading times and improved website performance.

How Does GD: AVIF Image Inclusion Impact PHP Developers?

The latest PHP 8.1 release integrated AVIF support into the GD library, thus providing developers with the ability to create, edit, and manipulate AVIF images. This integration can be beneficial for developers as it lets them reduce the size of images in their applications, thereby contributing to an improved user experience.

New AVIF Functions Introduced

With PHP 8.1, also we have some new functions. 

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

imagecreatefromavif function returns a new GdImage instance from a given AVIF image. For more details, you can check the PHP manual.

imageavif function outputs the file. For more details, you can check the PHP manual.

How to convert JPG to AVIF with PHP?

This is the basic example with the new imageavif function.

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