Search code examples
phpimagemagickheightwidthcrop

Imagemagick crop not working


I was trying to perform a crop on a image using imagemagick. It did not give the results I expected. I decided to set the crop value to 0 to try and find the error. With a crop value of 0 the image was still being cropped.

Here is the code:

$img = new Imagick();
$img->readImage("{german-grammar.pdf}[17]");
$img->trimImage(10);
$width = $img->getImageWidth();
$height= $img->getImageHeight();
$img->setImagePage($width,$height, 0, 0); //Solution
$img->cropImage($width, $height,0,0); //Original Problem lime
$img->setImagePage(0,0,0,0);
$img->writeImage($ImagesPath.$ImageName);

The input is a PDF file.

I would appreciate if anyone could tell me what I am doing wrong. Surely a crop value of 0 should not crop at all.

Thanks!

Cymro


Solution

  • I think you need to repage your image immediately after the trimImage() and before the cropImage(). It is generally a good idea to repage an image after any changes to its geometry (such as trimming and resizing) if you want the image to forget that it was once part of a larger image and go forth in the world happy and content with its own new shape and size.