Content-aware resizing with PHP & ImageMagick
In this post, we explore a powerful technique for resizing images in a content-aware manner using PHP and ImageMagick. By leveraging the liquid rescale feature available via the Imagick extension, you can intelligently adjust image dimensions while preserving key details.
What is content-aware resizing?
Content-aware resizing—often referred to as seam carving—allows you to adjust image dimensions by removing or inserting pixels in areas with less important visual information, rather than uniformly scaling the entire image. Unlike traditional resizing methods, this approach minimizes distortion of the subject in your images.
Prerequisites
Before getting started, ensure you have the following:
- A supported PHP 8.x release
- The Imagick extension enabled in your PHP installation
- ImageMagick version 6.5.3-10 or later (both version 6 and 7 are supported)
Additional prerequisites
For content-aware resizing functionality:
- liblqr library must be installed (package
liblqr-1-0on Debian/Ubuntu) - ImageMagick must be compiled with liblqr support
How to use liquid rescale in PHP
ImageMagick provides the liquidRescaleImage method, which performs content-aware resizing. Here's
how to implement it in a local CLI script with a trusted, single-frame JPEG:
<?php
$image = new Imagick();
try {
$image->readImage('input.jpg');
// Define your target dimensions
$targetWidth = 400;
$targetHeight = 300;
// Parameters for liquid rescale
$deltaX = 1; // Maximum sideways step of a seam
$rigidity = 0; // Penalty for bending a seam
// Perform content-aware resizing
$image->liquidRescaleImage($targetWidth, $targetHeight, $deltaX, $rigidity);
// Write the result
$image->writeImage('output.jpg');
} catch (ImagickException $e) {
fwrite(STDERR, "Image resizing failed.\n");
$failed = true;
} finally {
$image->clear();
}
if (isset($failed)) {
exit(1);
}
Parameter fine-tuning
The liquid rescale operation is controlled by two key parameters:
- deltaX: Maximum sideways step of a seam. Zero produces straight seams; larger values give seams more freedom to move around image content.
- rigidity: A nonnegative penalty for bending seams. Zero adds no penalty; larger values favor straighter seams. This does not guarantee that a face or other important feature is preserved.
These meanings come from the Imagick parameter documentation. Inspect the result on representative images when choosing values.
Performance considerations
Content-aware resizing can be resource-intensive, especially for large images. Here are some optimization strategies:
- Reduce input dimensions before seam carving when the original resolution is unnecessary
- Implement queue systems for batch processing
- Combine standard resizing with liquid rescale:
<?php
$image = new Imagick();
try {
$image->readImage('input.jpg');
// Initial standard resize
$image->resizeImage(800, 600, Imagick::FILTER_LANCZOS, 1, true);
// Follow with content-aware adjustment
$image->liquidRescaleImage(400, 300, 1, 0);
$image->writeImage('optimized-output.jpg');
} catch (ImagickException $e) {
fwrite(STDERR, "Image resizing failed.\n");
$failed = true;
} finally {
$image->clear();
}
if (isset($failed)) {
exit(1);
}
Common issues and solutions
Memory allocation errors
PHP's memory_limit does not reliably bound ImageMagick's native allocations. Configure
ImageMagick's resource limits and security policy,
including memory, disk cache, dimensions, and processing time, for your workload. Surface resource
limit failures instead of retrying the same oversized image indefinitely.
Missing Liblqr support
Verify installation with:
magick -list configure | grep -i lqr
For ImageMagick 6, use convert in place of magick. Look for lqr in the configured delegates.
The CLI and PHP extension can use different ImageMagick builds; check php --ri imagick as well.
If the build linked to Imagick lacks liblqr, install a build with that delegate and restart PHP.
Quality preservation
For significant size reductions:
- Apply multiple gradual resizing operations
- Combine with sharpening filters after resizing
- Maintain original aspect ratio when possible
Wrapping up
Content-aware resizing enables you to maintain image integrity while adjusting dimensions to focus on important areas. By combining PHP with ImageMagick's liquid rescale functionality, you can create adaptive image processing solutions that respond to visual content.
For enterprise-scale image processing needs, consider Transloadit's Image Processing API which handles complex workflows automatically.
Happy coding!
