All formats

Imagick

LanguagePHP (C extension)
LicensePHP License 3.01
Repositorygithub.com/Imagick/imagick
DocumentationPHP manual: Imagick
Depends onImageMagick 7 (or 6.9) built with libheif; libheif with libde265 (decode) and x265 (encode)
Read / writeyes / yes if libheif has an HEVC encoder (often read-only)

Imagick wraps ImageMagick’s MagickWand API. HEIC support comes from libheif, so it depends on three things: ImageMagick built with libheif, libheif with the libde265 decoder plugin, and for writing, the x265 encoder plugin. When they are present, .heic files open like JPEG or PNG. The typical use is converting photos uploaded from iPhones to JPEG.

Installation#

Shell
# Debian / Ubuntu
sudo apt install php-imagick libheif-plugin-libde265 libheif-plugin-x265
# macOS
brew install imagemagick && pecl install imagick

Check support:

PHP
<?php
var_dump(Imagick::queryFormats('HEIC'));  // ["HEIC"] when ImageMagick has libheif

queryFormats only shows that ImageMagick was built with libheif. If reading still fails with a “no decoder” or “unsupported codec” error, the libde265 plugin is missing.

Basic usage: convert HEIC to JPEG#

PHP
<?php
$im = new Imagick('IMG_0001.heic');
printf("%d x %d\n", $im->getImageWidth(), $im->getImageHeight());
$im->setImageFormat('jpeg');
$im->setImageCompressionQuality(90);
$im->writeImage('IMG_0001.jpg');

How to convert an uploaded HEIC file#

PHP
<?php
$im = new Imagick();
$im->readImageBlob(file_get_contents($_FILES['photo']['tmp_name']));
$im->autoOrient();          // upright, whatever the stored orientation
$im->setImageFormat('jpeg');
$im->setImageCompressionQuality(85);
file_put_contents('upload.jpg', $im->getImageBlob());

How to make a thumbnail from a HEIC photo#

PHP
<?php
$im = new Imagick('IMG_0001.heic');
$im->autoOrient();
$im->thumbnailImage(400, 400, true);  // fit in 400 x 400, strips metadata
$im->setImageFormat('jpeg');
$im->writeImage('thumb.jpg');

How to read Exif data#

PHP
<?php
$im = new Imagick();
$im->pingImage('IMG_0001.heic');  // reads the header and metadata, not the pixels
foreach ($im->getImageProperties('exif:*') as $name => $value) {
    echo "$name: $value\n";
}

How to save an image as HEIC#

PHP
<?php
$im = new Imagick('photo.png');
$im->setImageFormat('heic');
$im->setCompressionQuality(50);
$im->writeImage('photo.heic');  // fails if libheif has no HEVC encoder

Tips#