The AVIF Mime Type
How to use the AVIF image format in PHP; A1 or AVIF is a new image format that offers better compression than WebP, JPEG and PNG, and that already works in Google Chrome.

By. Jacob
Edited: 2026-10-16 10:15
The MIME type (also known as the Content-Type) for AVIF images is image/avif, and the standard file extension is .avif.
AVIF stands for AV1 Image File Format. It is a modern image format based on the AV1 video compression technology, designed to provide efficient image compression while maintaining good visual quality.
AVIF generally produces smaller files than JPEG and can also outperform WebP and PNG. However, the actual savings depend on image content, dimensions, encoder settings, and the quality level used during conversion.
In my own tests, I found that AVIF images could be approximately 20% smaller than WebP and almost 50% smaller than JPEG. These numbers are observations from my tests rather than guaranteed compression ratios.
AVIF supports both lossy and lossless compression, alpha transparency, animation, and higher color depths. It is supported by modern versions of Chrome, Firefox, Safari, and Edge.
Serving AVIF images with the correct Content-Type
A typical HTTP response for an AVIF image may look like this:
HTTP/1.1 200 OK
Content-Type: image/avif
Content-Length: 14200
To deliver an image/avif Content-Type from PHP, you can use the header() function:
header('Content-Type: image/avif');
readfile('/var/www/mysite/images/my-image.avif');
exit();
This tells the browser that the response contains an AVIF image. When serving static files directly through Apache or Nginx, the web server should normally handle the MIME type automatically, provided it is configured correctly.
Converting images to AVIF
There are several ways to convert images to AVIF, including using ImageMagick or PHP's GD Library.
Converting with ImageMagick
ImageMagick supports AVIF conversion when it has been built with the necessary codec support. With ImageMagick 7, you can convert a JPEG image using the magick command:
magick my-image.jpg -quality 50 my-image.avif
The -quality option controls the encoding quality. Lower values generally produce smaller files, but may introduce more visible compression artifacts.
On systems running ImageMagick 6, the equivalent command is typically:
convert my-image.jpg -quality 50 my-image.avif
Both examples require an ImageMagick installation that supports writing AVIF files.
Converting with ImageMagick from PHP
You can also execute ImageMagick commands directly from PHP using shell_exec():
$source = '/var/www/mysite/images/my-image.jpg';
$destination = '/var/www/mysite/images/my-image.avif';
$command = 'magick '
. escapeshellarg($source)
. ' -quality 50 '
. escapeshellarg($destination)
. ' 2>&1';
$output = shell_exec($command);
Using escapeshellarg() for each file path is important when constructing shell commands, particularly if the paths originate from user input.
Checking whether a file exists is not a substitute for escaping shell arguments. A filename may contain spaces or shell metacharacters even when it refers to a valid file.
Also remember that shell_exec() does not provide a reliable exit status. For production applications, you should check whether the conversion succeeded and whether the destination file was created correctly. The exec() function can be used when you need the command's exit code.
Converting directly with PHP GD
Starting with PHP 8.1, the GD Library supports reading and writing AVIF images, provided PHP was built with AVIF support.
You can check whether AVIF is available using:
if (imagetypes() & IMG_AVIF) {
echo 'AVIF is supported';
}
To convert a JPEG image into AVIF directly with PHP:
$image = imagecreatefromjpeg('my-image.jpg');
if ($image === false) {
throw new RuntimeException('Unable to load image');
}
if (!imageavif($image, 'my-image.avif', 50)) {
throw new RuntimeException('Unable to save AVIF image');
}
imagedestroy($image);
The third argument to imageavif() specifies the quality, ranging from 0 to 100. PHP also supports an optional fourth argument to control encoding speed.
GD is convenient when you are already processing images in PHP, although its available encoding options may be more limited than those offered by dedicated image encoders.
Note. You can also use Beamtic's file handler class to automate image delivery. The class can serve alternative image formats and redirect compatible clients to AVIF versions where available.
AVIF compression test results
To investigate how much disk space could be saved by converting existing images to AVIF, I performed several conversions using ImageMagick on Linux.
For example:
magick my-image.jpg -quality 50 my-image.avif
My tests suggested that AVIF could reduce file sizes by approximately 20% compared with WebP and almost 50% compared with JPEG.
However, these figures should be treated as illustrative rather than universal. Compression results vary considerably between photographs, graphics, screenshots, and images that have already been optimized.
It is also important to compare images at approximately equivalent visual quality. Using the same numerical quality setting for different formats does not necessarily produce comparable image quality.
Converting PNG files
When I attempted to convert PNG files directly to AVIF, some of the resulting files were actually larger than the originals.
This is not necessarily an error in the conversion process. PNG uses lossless compression and can be particularly efficient for simple graphics, illustrations with limited colors, and certain types of screenshots.
AVIF compression behaves differently, and its efficiency depends on the image content and encoding settings. As a result, some PNG images may become significantly smaller when converted, while others may show little improvement or even increase in size.
In some of my experiments, using WebP as an intermediate format before further conversion appeared to produce smaller files. However, this does not establish that intermediate conversion is inherently more efficient. It may instead reflect differences in encoder behavior or the introduction of additional lossy compression.
Because of the inconsistent results with PNG images, I decided to disable automatic PNG-to-AVIF conversion in my file handler class.
This is a conservative approach, although a more flexible implementation could generate an AVIF version and only use it when the resulting file is actually smaller.
It is also worth remembering that PNG files may already have been optimized using tools such as optipng, pngcrush, or advpng.
Which images should be converted to AVIF?
Although AVIF offers excellent compression, converting every image on a website is not necessarily a good idea.
JPEG and WebP
Photographs and other complex images are usually good candidates for AVIF conversion. These types of images often benefit significantly from modern lossy compression.
However, if an existing WebP or JPEG image is already small and visually acceptable, the additional savings may not justify the processing time or storage required for another version.
PNG
PNG files should be evaluated individually. Photographic PNG images may benefit considerably from AVIF, while small icons, screenshots, and simple graphics may not.
Both WebP and AVIF support alpha transparency, so transparency alone is not a reason to avoid conversion.
However, if exact pixel reproduction is important, make sure that you use a suitable lossless encoding mode rather than lossy compression.
GIF and animated images
Both WebP and AVIF support animation, making them possible alternatives to animated GIF files.
Animated AVIF is supported by modern browsers, but animation requires additional consideration when converting files. The conversion tool must preserve the frames, timing, and looping behavior of the original animation.
I have not personally tested animated AVIF conversion extensively, so I currently prefer to handle animated files separately instead of automatically converting them.
SVG
SVG is a vector-based image format, unlike raster formats such as JPEG, PNG, WebP, and AVIF.
Vector graphics can scale to different dimensions without losing sharpness, which makes SVG particularly suitable for icons, logos, and illustrations.
Converting SVG to AVIF would rasterize the image, removing its resolution-independent scaling advantages. For this reason, SVG files should generally be preserved in their original format unless there is a specific reason to convert them.
Should you automatically convert images to AVIF?
For a website that serves many photographs or other complex images, AVIF can provide substantial bandwidth and storage savings.
However, automatic conversion should ideally take several factors into account:
- The size of the original image compared with the converted image.
- The visual quality of the resulting image.
- The time and resources required for encoding.
- Whether the image contains transparency or animation.
- Whether the client's browser supports AVIF.
Modern browsers generally support AVIF, but providing a WebP, JPEG, or PNG fallback can still be useful for compatibility with older clients.
A practical approach is to generate alternative image formats when an image is uploaded, cache the results, and serve the most appropriate version to each client.
Rather than converting every image indiscriminately, I recommend comparing the resulting file sizes and retaining the original when AVIF offers no meaningful advantage.

Tell us what you think: