coldfingertips has asked for the wisdom of the Perl Monks concerning the following question:

I'm not very familiar with Image::Magick but here's a question for your experts.

I'm creating an image gallery and I want to thumbnail images for the category view. I don't want all of them to be 100x100 blocks as that'll mis-proportion some of them.

Can someone help me figure out how to have a $max_h and $max_w determine the size of the thumbnail whereas both sides must be less than or equal to (obviously the one side of the image should match with one of the max_h or max_w) all the while keeping the image in proportion?

Replies are listed 'Best First'.
Re: Thumbnailing with image::magick
by wind (Priest) on Aug 21, 2007 at 01:09 UTC

    Yes, Image::Magick will easily scale images and maintain the size proportions if you use the Scale method. Here's how you would accomplish that:

    # Thumbnail Dimensions my ($thumb_max_height, $thumb_max_width) = (100,100); my $p = new Image::Magick; $p->Read($image_file); $p->Scale(geometry => "${thumb_max_height}x${thumb_max_width}"); $p->Write($thumb_file);

    - Miller

Re: Thumbnailing with image::magick
by kyle (Abbot) on Aug 21, 2007 at 00:58 UTC

    Figuring out how to keep a scaled image proportional shouldn't be too hard.

    my $new_width = $orig_width; my $new_height = $orig_height; if ( $new_width > $max_width ) { $new_height = int( $orig_height / ( $orig_width / $max_width ) ); $new_width = $max_width; } if ( $new_height > $max_height ) { $new_width = int( $orig_width / ( $orig_height / $max_height ) ); $new_height = $max_height; }

    I wouldn't be surprised if Image::Magick has a way to do this for you.

Re: Thumbnailing with image::magick
by spatterson (Pilgrim) on Aug 23, 2007 at 10:49 UTC
    When used from the command line, ImageMagick preserves the aspect ratio, so a resize to 100x100 reduces the larger dimension to 100 pixels and reduces the smaller dimension in proportion.

    It should probably do the same through perl.


    just another cpan module author