Trimming an image - Imagick : Create and modify images : Graphics Image PHP Source Code


PHP Source Code » Graphics Image » Create and modify images »

 

Trimming an image - Imagick


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 source image is a simple png image with black circle on gray background:
test image

The trimmed image:
result image

<?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);
/* Ouput the image */
header"Content-Type: image/" $im->getImageFormat() );
echo 
$im;
?>


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Graphics Image
» Create and modify images