(PECL imagick 2.0.0)

Imagick::trimImageRemove edges from the image


bool Imagick::trimImage ( float $fuzz )

Remove edges that are the background color from the image. Цей метод доступний якщо Imagick був зібраний з версією ImageMagick 6.2.9 або новішою.



By default target must match a particular pixel color exactly. However, in many cases two colors may differ by a small amount. The fuzz member of image defines how much tolerance is acceptable to consider two colors as the same. This parameter represents the variation on the quantum range.

Значення, що повертаються

Повертає TRUE в разі успіху.


Кидає ImagickException в разі помилки.


Приклад #1 Using Imagick::trimImage():

Trim an image, then display to the browser.

/* Create the object and read the image in */
$im = new Imagick("image.jpg");

/* Trim the image. */

/* Ouput the image */
header("Content-Type: image/" $im->getImageFormat());

Прогляньте Також

add a note add a note

User Contributed Notes 3 notes

14 years ago
After operations that change the crop of the image, like trimImage does, IM preserves the old canvas and positioning info. If you need to do additional operations on the image based on the new size, you'll need to reset this info with setImagePage. This is the equivalent of the +repage command line argument.

$im->setImagePage(0, 0, 0, 0);
Kirsty Forrester
10 years ago
The command for trim can return info which tells you the coordinates of where the image was cropped and the new dimensions of the trimmed image. I couldn't find an example for how to do that with this class, so here's how I did it:


// Create and trim image
$image = new Imagick('image.png');

* Imagick:: getImagePage() gives us an array with the width, height, x and y coordinates of the trimmed image
* However, the width and height are of the original image
$imagePage = $image->getImagePage();
$x, $y) = array($imagePage['x'], $imagePage['y']);

// So we need to reset the image page to get the right height and width of the trimmed image
$image->setImagePage(0, 0, 0, 0);
$width, $height) = array($image->width, $image->height);


This will leave you with two variables for the width and height of the trimmed image, plus two variables for the x-coordinate and y-coordinate of the trimmed area relative to the original image.

This information is useful when the image you're trimming is the difference between two images (e.g. from Imagick::compareImages(), and you want to crop the original image to the same size and position as the difference.
Mark van der Velden
15 years ago
This method requires ImageMagick version >= 6.2.8
To Top