How can I resize an image to fit area with Image::Magick?

Marquis Wang picture Marquis Wang · May 21, 2009 · Viewed 8.1k times · Source

From command line with imagemagick, you can use:

convert dragon.gif -resize 64x64^ -gravity center -extent 64x64 fill_crop_dragon.gif

to resize and then crop an image so that it fills the area as much as possible.

How do I do this from Perl's Image::Magick?

Answer

Sinan Ünür picture Sinan Ünür · May 21, 2009
#!/usr/bin/perl

use strict;
use warnings;

use Image::Magick;

my $image = Image::Magick->new;
$image->read('test.jpg');

$image->Set( Gravity => 'Center' );
$image->Resize( geometry => '64x64' );
$image->Extent( geometry => '64x64' );
$image->Write( 'test-out.jpg' );

See the PerlMagick documentation.