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:

$image->Set(Gravity => 'Center');
If you add that before the Crop the above function works like I desired!

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
    sub thumbnail{ # receives: # file source (absolute path on machine) # file target out (absolute path on machine) # restriction # restriction type # quality my ($src,$out,$restriction,$restrict_type,$quality)=@_; # both restrict to height and width :both won't go ov +er this.. # width restrict to width # height restrict to height # square square - will crop sides or top and bottom to +fit. neat. my $img = Image::Magick->new; #blank $img->Read($src); #read source file into blank unless($img){return 0;} my ($w,$h) = $img->Get('width','height'); # find dimensions unless($w and $h){return 0;} # # # #if width is bigger then height, we use restrict to w if ($restrict_type eq 'both'){ if ($w>$h){ $restrict_type='width'; } else { $restrict_type='height'; } } my $newh; my $neww; # initialize new dimensions # not used if +'square' if ($restrict_type eq 'height') { $neww=(($w*$restriction)/$h); $img->Resize(width=>$neww, height=>$restriction); } if ($restrict_type eq 'width') { $newh=(($h*$restriction)/$w); $img->Resize(width=>$restriction, height=>$newh); } if ($restrict_type eq 'square'){ # my ($cut,$xcut,$ycut); if ($w>$h){ $cut=$h; $xcut=(($w-$h)/2); $ycut=0; } if ($w<$h){ $cut=$w; $xcut=0; $ycut=(($h-$w)/2); } $img->Crop(width=>$cut,height=>$cut,x=>$xcut,y=>$ycut) +; $img->Resize(width=>$restriction, height=>$restriction +); # } $img->Set(quality=>$quality); $img->Write($out); undef $img; return 1; }

    Usage: for a square thumb 80px each side:

    $image_in=$ENV{DOCUMENT_ROOT}.'/img/1.jpg'; #example, might have ENV D +OC ROOT on your server, need full pathof some sort though $image_out=$ENV{DOCUMENT_ROOT}.'/img/.thumb.1.jpg'; #whatever you want + to call it. thumbnail($image_in, $image_out,80,'square',90);

    update: I made a module to make a square image as we talk abot here, Image::Magick::Square . And another to automatically make thumbnails server side , with square option, Image::Magick::Thumbnail::NotFound