Last updated: August 28, 2026.
AVIF can produce small files, but encoding is CPU-intensive. Generate it once during upload or in a background job rather than on every request.
Convert a JPEG to AVIF
<?php
$image = imagecreatefromjpeg(__DIR__ . '/photo.jpg');
if ($image === false) throw new RuntimeException('Cannot decode JPEG.');
if (!function_exists('imageavif')) throw new RuntimeException('AVIF is unavailable.');
if (!imageavif($image, __DIR__ . '/photo.avif', 55, 6)) throw new RuntimeException('Cannot create AVIF.');
imagedestroy($image);Choose fallbacks
- Test quality settings on representative images.
- Keep a WebP or JPEG fallback when required.
- Serve the result with the image/avif content type.
- Measure encoding time on the production server.
Reference: PHP imageavif reference.