Skip to content

marko/media-imagick

ImageMagick image processing for marko/media --- resize, crop, and convert images with superior quality, AVIF support, and ICC color profile handling. Provides an ImageProcessorInterface implementation backed by the Imagick PHP extension, delivering higher-quality resampling (Lanczos filter), broader format support including AVIF and WebP, and ICC color profile preservation --- advantages over the GD-based driver for production image pipelines. Requires ext-imagick installed separately via PECL.

Terminal window
composer require marko/media-imagick

Requirement: The Imagick PHP extension must be installed before use:

Terminal window
pecl install imagick

If the extension is absent, ImagickImageProcessor throws ImagickProcessingException on construction.

use Marko\MediaImagick\Driver\ImagickImageProcessor;
$processor = new ImagickImageProcessor();
$outputPath = $processor->resize(
imagePath: '/path/to/image.jpg',
width: 800,
height: 600,
maintainAspect: true,
);

Set maintainAspect: false to force exact dimensions without preserving the aspect ratio.

use Marko\MediaImagick\Driver\ImagickImageProcessor;
$processor = new ImagickImageProcessor();
$outputPath = $processor->crop(
imagePath: '/path/to/image.jpg',
x: 100,
y: 50,
width: 400,
height: 300,
);

AVIF is the key differentiator over the GD driver --- it produces smaller files with better quality than WebP or JPEG, and is fully supported by Imagick:

use Marko\MediaImagick\Driver\ImagickImageProcessor;
$processor = new ImagickImageProcessor();
$outputPath = $processor->convert(
imagePath: '/path/to/image.jpg',
format: 'avif',
);
use Marko\MediaImagick\Driver\ImagickImageProcessor;
$processor = new ImagickImageProcessor();
$outputPath = $processor->convert(
imagePath: '/path/to/image.png',
format: 'webp',
);

Produces a square-bounded thumbnail fitting within maxDimension on its longest side:

use Marko\MediaImagick\Driver\ImagickImageProcessor;
$processor = new ImagickImageProcessor();
$outputPath = $processor->thumbnail(
imagePath: '/path/to/image.jpg',
maxDimension: 150,
);

Depend on the interface from marko/media, not the concrete class:

use Marko\Media\Contracts\ImageProcessorInterface;
public function __construct(
private ImageProcessorInterface $imageProcessor,
) {}
FormatNotes
JPEGFull read/write support
PNGFull read/write support
WebPFull read/write support
GIFFull read/write support
AVIFFull read/write support (key advantage over GD)
TIFFFull read/write support
BMPFull read/write support
HEICRead/write (requires libheif)

Additional formats depend on the libraries linked against your ImageMagick build. Run convert -list format on the command line to see your full list.

Featuremarko/media-imagickmarko/media-gd
Resize qualityLanczos filterBicubic
AVIF supportYesNo
ICC color profilesPreservedDropped
Format support100+ formatsJPEG, PNG, GIF, WebP
Memory usageModerateLower

Choose marko/media-gd when ext-gd is sufficient and memory is constrained. Choose marko/media-imagick when quality, AVIF, or broad format support matters.

use Marko\MediaImagick\Driver\ImagickImageProcessor;
public function resize(string $imagePath, int $width, int $height, bool $maintainAspect = true): string;
public function crop(string $imagePath, int $x, int $y, int $width, int $height): string;
public function convert(string $imagePath, string $format): string;
public function thumbnail(string $imagePath, int $maxDimension): string;

All methods return the absolute path to the processed output file in the system temp directory. All methods throw ImagickProcessingException on failure.