debiandude has asked for the wisdom of the Perl Monks concerning the following question:
Right now I use Image::Magick to create thumbnails for my website. However, since it would look nicer, I want a square thumbnail. Since the original images are not square, I first resize the image and then I crop it. But it always crops from the lower left. I would like it to crop from the center of the image. If I did this on the command line I would use the gravity option, i.e.:
mogrify crop 200x200+0+0 -gravity center file.jpg
However, looking at the docs it seems that Crop does not have the gravity option. Any clues? Thanks.
This is what I am currently doing:
my $image = Image::Magick->new; $image->Read($file); my ($width, $height) = $image->Get('width', 'height'); if($width < $height) { my $nh = $size*$height/$width; $image->Resize(width => $size, height => $nh); } else { my $nw = $size*$width/$height; $image->Resize(width => $nw, height => $size); } $image->Crop(width => $size, height => $size);
Edit: I found what I needed. To set the gravity you do this:
If you add that before the Crop the above function works like I desired!$image->Set(Gravity => 'Center');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Make a Square Thumbnail
by GrandFather (Saint) on Jun 08, 2006 at 04:28 UTC | |
|
Re: Make a Square Thumbnail
by leocharre (Priest) on Jun 08, 2006 at 15:27 UTC |