Last updated: February 4, 2025

<span aria-hidden="true" id="content-aware-resizing-with-php--imagemagick"></span>

# Content-aware resizing with PHP & ImageMagick

![Tim Koschützki](/assets/images/teammates/avatar-tim-kos-1.jpg?dpl=dpl_C6YH6XrtnwbHLJcm1LDKywQn4CB3)

#### Tim Koschützki

Co-founder · Berlin, Germany · Show bio

[](https://x.com/tim%5Fkos)[](https://github.com/tim-kos)

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.

<span aria-hidden="true" id="what-is-content-aware-resizing"></span>

## 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.

<span aria-hidden="true" id="prerequisites"></span>

## Prerequisites

Before getting started, ensure you have the following:

* PHP 7.4 or later (tested up to PHP 8.3)
* The Imagick extension enabled in your PHP installation
* ImageMagick version 6.5.3-10 or later (both version 6 and 7 are supported)

<span aria-hidden="true" id="additional-prerequisites"></span>

### Additional prerequisites

For content-aware resizing functionality:

* liblqr library must be installed (package `liblqr-1-0` on Debian/Ubuntu)
* ImageMagick must be compiled with liblqr support

<span aria-hidden="true" id="how-to-use-liquid-rescale-in-php"></span>

## How to use liquid rescale in PHP

ImageMagick provides the `liquidRescaleImage` method, which performs content-aware resizing. Here's how to implement it:

```php
<?php
try {
    // Create a new Imagick object from an input image
    $image = new Imagick('input.jpg');

    // Define your target dimensions
    $targetWidth = 400;
    $targetHeight = 300;

    // Parameters for liquid rescale
    $deltaX = 1;     // Controls horizontal pixel movement (1-100)
    $rigidity = 0;   // Determines resistance to pixel adjustment (0-100)

    // Perform content-aware resizing
    $image->liquidRescaleImage($targetWidth, $targetHeight, $deltaX, $rigidity);

    // Write the result
    $image->writeImage('output.jpg');

    $image->destroy();
} catch (ImagickException $e) {
    echo "Error: " . $e->getMessage();
}

```

<span aria-hidden="true" id="parameter-fine-tuning"></span>

## Parameter fine-tuning

The liquid rescale operation is controlled by two key parameters:

* **deltaX** (1-100): Controls horizontal pixel movement
  * Lower values (1-10) enable more aggressive seam removal
  * Higher values preserve original image structure
* **rigidity** (0-100): Determines resistance to pixel adjustment
  * 0 allows maximum flexibility for content-aware resizing
  * Higher values protect important features from distortion

<span aria-hidden="true" id="performance-considerations"></span>

## Performance considerations

Content-aware resizing can be resource-intensive, especially for large images. Here are some optimization strategies:

* Process images in smaller sections for memory efficiency
* Implement queue systems for batch processing
* Combine standard resizing with liquid rescale:

```php
try {
    $image = new Imagick('input.jpg');

    // Initial standard resize
    $image->resizeImage(800, 600, Imagick::FILTER_LANCZOS, 1);

    // Follow with content-aware adjustment
    $image->liquidRescaleImage(400, 300, 1, 0);

    $image->writeImage('optimized-output.jpg');
    $image->destroy();
} catch (ImagickException $e) {
    echo "Error: " . $e->getMessage();
}

```

<span aria-hidden="true" id="common-issues-and-solutions"></span>

## Common issues and solutions

<span aria-hidden="true" id="memory-allocation-errors"></span>

### Memory allocation errors

```php
ini_set('memory_limit', '512M');  // Adjust based on image size

```

<span aria-hidden="true" id="missing-liblqr-support"></span>

### Missing Liblqr support

Verify installation with:

```bash
convert -list configure | grep LIBLQR

```

If no output appears, reinstall ImageMagick with liblqr support.

<span aria-hidden="true" id="quality-preservation"></span>

### Quality preservation

For significant size reductions:

* Apply multiple gradual resizing operations
* Combine with sharpening filters after resizing
* Maintain original aspect ratio when possible

<span aria-hidden="true" id="wrapping-up"></span>

## 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](/services/image-processing.md) which handles complex workflows automatically.

Happy coding!

\#php#imagemagick#imagick#content-aware-resizing#image-processing-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
