Imagick
| Language | PHP (C extension) |
|---|---|
| License | PHP License (extension), ImageMagick License (Apache-2.0 style) |
| Repository | github.com/Imagick/imagick |
| Documentation | Imagick on php.net |
| Depends on | ImageMagick 7 or 6.9 built with libheif, which needs dav1d or libaom (decode) and libaom, SVT-AV1 or rav1e (encode) |
| Read / write | yes / yes, alpha, 8/10/12-bit, Exif and ICC |
Imagick is the PHP extension for ImageMagick. ImageMagick handles AVIF in its HEIC coder through libheif, so AVIF support depends on the ImageMagick build on the server, not on Imagick itself. Most Linux distributions build ImageMagick with libheif; whether AVIF writing works also depends on libheif having an AV1 encoder plugin installed.
Compared to GD, Imagick keeps metadata and color profiles, resizes with better filters and handles higher bit depths.
Installation#
Shell
# Debian / Ubuntu
sudo apt install php-imagick libheif-plugin-dav1d libheif-plugin-aomenc
# macOS / others
pecl install imagick
Check support:
PHP
<?php
print_r(Imagick::queryFormats('AVIF')); // Array ( [0] => AVIF ) when supported
magick -list format | grep -i avif shows whether the format is readable and writable (rw+).
Basic usage: convert JPEG to AVIF#
PHP
<?php
try {
$image = new Imagick('photo.jpg');
printf("%d x %d\n", $image->getImageWidth(), $image->getImageHeight());
$image->setImageFormat('avif');
$image->setImageCompressionQuality(60); // 0..100
$image->writeImage('photo.avif');
$image->clear();
} catch (ImagickException $e) {
echo 'error: ', $e->getMessage(), "\n";
}
How to set AVIF encoding speed and chroma subsampling#
ImageMagick reads heic: options for the AVIF encoder:
PHP
<?php
$image = new Imagick('screenshot.png');
$image->setOption('heic:speed', '6'); // 0 (slowest, smallest) .. 9
$image->setOption('heic:chroma', '444'); // 420, 422 or 444
$image->setImageFormat('avif');
$image->setImageCompressionQuality(70);
$image->writeImage('screenshot.avif');
How to resize and convert to AVIF#
PHP
<?php
$image = new Imagick('large.jpg');
$image->thumbnailImage(800, 0); // 800 pixels wide, height keeps aspect ratio
$image->stripImage(); // drop metadata
$image->setImageFormat('avif');
$image->setImageCompressionQuality(55);
file_put_contents('thumb.avif', $image->getImageBlob());
How to get AVIF width and height without decoding#
pingImage reads the header without decoding pixels:
PHP
<?php
$image = new Imagick();
$image->pingImage('photo.avif');
printf("%d x %d\n", $image->getImageWidth(), $image->getImageHeight());
How to convert AVIF to JPEG#
PHP
<?php
$image = new Imagick('photo.avif');
$image->setImageBackgroundColor('white');
$image = $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); // remove transparency
$image->setImageFormat('jpeg');
$image->setImageCompressionQuality(85);
$image->writeImage('photo.jpg');
Tips#
- Quality 100 gives lossless AVIF.
- Limit memory for untrusted uploads with
Imagick::setResourceLimit(Imagick::RESOURCETYPE_MEMORY, 256 * 1024 * 1024). - To create animated AVIF, use
avifencor FFmpeg from the command line.