All formats

Imagick

LanguagePHP (PECL extension)
LicensePHP License 3.01 (ImageMagick: ImageMagick License)
Repositorygithub.com/Imagick/imagick
Documentationphp.net/manual/en/book.imagick.php
Depends onImageMagick 6 or 7 (MagickWand) with JPEG support
Read / writeyes / yes; baseline, progressive, CMYK, sampling factors; Exif reading, ICC and metadata kept on save

Imagick exposes ImageMagick’s MagickWand API as the Imagick class. For JPEG it offers what GD lacks: auto-orientation, better resize filters, chroma subsampling control, keeping or converting ICC profiles, CMYK support, and the jpeg:size hint that decodes large photos at reduced scale.

Unlike GD, ImageMagick keeps Exif and other profiles when writing unless you call stripImage(). Configure ImageMagick’s policy.xml resource limits on servers that process uploads.

Installation#

Shell
# Debian / Ubuntu
sudo apt install php-imagick
# macOS (Homebrew PHP)
brew install imagemagick && pecl install imagick

Basic usage: auto-orient, resize and save a JPEG#

PHP
<?php
$im = new Imagick('photo.jpg');
printf("%d x %d\n", $im->getImageWidth(), $im->getImageHeight());

$im->autoOrient();                                  // apply the Exif orientation
$im->resizeImage(1200, 0, Imagick::FILTER_LANCZOS, 1);  // height 0 = keep aspect ratio
$im->setImageCompressionQuality(85);
$im->setInterlaceScheme(Imagick::INTERLACE_PLANE);  // progressive
$im->writeImage('out.jpg');
$im->clear();

How to get JPEG dimensions without decoding#

pingImage reads only the header:

PHP
$im = new Imagick();
$im->pingImage('photo.jpg');
printf("%d x %d, quality %d\n", $im->getImageWidth(), $im->getImageHeight(), $im->getImageCompressionQuality());

How to read Exif data#

PHP
$im = new Imagick('photo.jpg');
foreach ($im->getImageProperties('exif:*') as $name => $value) {
    echo "$name: $value\n";    // exif:Model, exif:DateTimeOriginal, exif:GPSLatitude, ...
}
echo $im->getImageProperty('exif:Model'), "\n";
echo $im->getImageOrientation(), "\n";   // Imagick::ORIENTATION_* constant, 1-8

How to set chroma subsampling and strip metadata#

PHP
$im = new Imagick('photo.jpg');
$im->stripImage();                                  // remove Exif, XMP, comments, ICC
$im->setSamplingFactors(['1x1', '1x1', '1x1']);     // 4:4:4; ['2x2','1x1','1x1'] = 4:2:0
$im->setImageCompressionQuality(90);
$im->writeImage('clean.jpg');

How to make thumbnails of large JPEGs quickly#

Set jpeg:size before reading so libjpeg decodes at ½, ¼ or 1/8 scale:

PHP
$im = new Imagick();
$im->setOption('jpeg:size', '600x600');
$im->readImage('big.jpg');
$im->autoOrient();
$im->thumbnailImage(300, 300, true);   // fit inside 300x300, strips profiles
$im->writeImage('thumb.jpg');

Tips#