Especially product images usually “suffer” from this issue; the product itself is composited on a white background and there are large areas of white around the object.
This is a simple example to demonstrate how to easily trim the areas off the image and only display the parts where the object lies.
Imagick::trimImage takes one parameter which is “fuzz”. Quoting ImageMagick manual: “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. For example, set fuzz to 10 and the color red at intensities of 100 and 102 respectively are now interpreted as the same color for the purposes of the floodfill.”
I use fuzz 0 in this example because the background color pixels are all same color.
- <?php
- /* Create the object and read the image in */
- $im = new Imagick( "test.png" );
- /* The background color. This is what we trim. */
- $im->setImageBackgroundColor( new ImagickPixel( "rgb(213,213,213)" ) );
- /* Trim the image. */
- $im->trimImage( 0 );
- /* Ouput the image */
- header( "Content-Type: image/" . $im->getImageFormat() );
- echo $im;
- ?>
The source image is a simple png image with black circle on gray background:

The trimmed image:

Trackback: PHPDeveloper.org
Pingback: developercast.com » Mikko Koppanen’s Blog: Trimming an image
#1 by Goran Jurić on August 5, 2008 - 12:10 pm
Quote
Do you know what are the dependencies for the Imagick::trimImage() method?
On my Windows box I have:
- Imagick 2.2.0b2-dev
- ImageMagick 6.4.1 06/14/08 Q8
and it works fine. But on my production machine (CentOS) I have:
- Imagick 2.2.0
- ImageMagick 6.2.8 04/17/08 Q16
and I get a “Fatal error: Call to undefined method Imagick::trimImage() in …” trying to call the trimImage() method.
#2 by Mikko Koppanen on August 5, 2008 - 12:39 pm
Quote
Goran,
if I remember correctly ImageMagick newer than 6.2.8 (not including 6.2.8).
#3 by Goran Jurić on August 6, 2008 - 12:43 am
Quote
Thank you for the quick reply. Upgrading ImagaMagick solved the problem.
Regards!
#4 by Andrei on August 22, 2009 - 11:56 am
Quote
How to trimming an image from one side (top or left or …)?
Thank you.
Pingback: Imagick blog » Blog Archive » Trimming an image
#5 by John on May 8, 2012 - 3:01 pm
Quote
I have tried using trimImage( 0 ), as described above with ImageMagick 6.7.6-8. However, the setImageBackgroundColor is ignored & all continuous colour is trimmed (I tested black/white/yellow combinations). I have had to workaround by putting contrasting pixels in areas I did not want trimmed.
I did try flattenImages() & setBackgroundColor also.
Any advice?
#6 by John on May 8, 2012 - 3:28 pm
Quote
Tested again & curiously it does not fully trim but leaves a 4px border…
#7 by Andreas on June 7, 2012 - 3:11 pm
Quote
Same problem.
Using the command line works fine:
convert image.jpg -deskew 40% -fuzz 50% -trim +repage -monochrome result.jpg
Using PHP with the same setting leaves a border.