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!


In reply to Make a Square Thumbnail by debiandude

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.