Page 1 of 1

Cropping the same image twice

Posted: 2009-12-26T13:18:03-07:00
by Ayame
I've just recently started to experiment with ImageMagick in PHP through the Imagick class, but stumbled across an oddity. I'm not sure whether it's a bug or not - it's probably my code - so I was wondering if someone can give me some insight in the situation.

As it stands, I need to crop a portion of an image and then on that cropped image crop it again. This in order to generate 2 thumbnails of a different size for file storage.
The problem is when I use the cropImage function on the imagemagick object the first time, it works like a charm. The second time however, I get 1x1 pixel results.

Short example

Code: Select all

$imagick = new Imagick($src_url);

// Do stuff to calculate x and y 
$imagick->cropImage($largeThumbWidth,$largeThumbHeight,$x,$y);
$imagick->writeImage($dst_url .'largethumb'. $dst_filename . '.jpg');

// Now make the small thumb, I fixed the values to be sure 
$imagick->cropImage(75,75,0,0);
$imagickthumb->writeImage($dst_url .'smallthumb'. $dst_filename . '.jpg');	

If I execute the code, the largethumbs are generated perfectly, but the smallthumbs are all 1x1. The $largeThumbWidth,$largeThumbHeight,$x,$y have values within the image range as the largethumbs are nicely generated, however if replace x and y with 0,0 the script works. I can go with the index up to the value of the width and height of the smallthumbnail, which is 75.

I do not want the coordinates to be 0,0 as some randomness is nice for the image gallery this is reaquired.
Does anyone see where I went wrong?

Thanks!
Ayame

Re: Cropping the same image twice

Posted: 2009-12-26T14:14:37-07:00
by fmw42
I don't know Imagick, but you probably should be posting this topic to its forum at viewforum.php?f=18

The other thing is that one usually applies +repage after a crop to remove the virtual canvas on the output. But I don't know what the Imagick equivalent it.

In your case your second crop appears to be applied to the output of the first crop and not to the original image. I am not sure what you want. But without the +repage, the first crop will have the virtual canvas and then the second crop on the first cropped image will be off from what you think you want. So either add the +repage to the first crop or crop the original image instead if that is what you really want.

Re: Cropping the same image twice

Posted: 2009-12-28T12:05:17-07:00
by Ayame
Many thanks, that actually solved it!

All I needed to do was add:
$imagick->setImagePage(0,0,0,0);