in reply to Creating thumbnails

I never used GD before today, so I made a little program to compare the output given by GD and Image::Magick. Here is the code:

#!/usr/bin/perl use strict; use warnings; use GD; use Image::Magick; use vars qw( $default_orientation $default_width $default_height $file +name ); $default_orientation = 'vertical'; $default_width = 150; $default_height = 100; $filename = 'image.jpg'; thumb_with_imagemagick($filename, 'im.jpg'); thumb_with_gd($filename, 'gd.jpg'); exit; sub thumb_with_imagemagick { my ($infile, $outfile, $image, $error, $x, $y, $size, $format, $width, $height); ($infile, $outfile) = @_; $image = Image::Magick->new; $error = $image->Read($infile); if (!$error) { ($x, $y, $size, $format) = $image->Ping("$infile"); ($width, $height) = thumbnail_dimensions($x, $y); $image->Scale(width=>$width, height=>$height); $error = $image->Write($outfile); } return $error; } sub thumb_with_gd { my ($infile, $outfile, $image, $error, $x, $y, $width, $height, $thumb); ($infile, $outfile) = @_; $image = GD::Image->new($infile); ($x, $y) = $image->getBounds(); ($width, $height) = thumbnail_dimensions($x, $y); $thumb = new GD::Image($width, $height); $thumb->copyResized($image, 0,0 , 0,0 , $width,$height , $x,$y +); open(IMAGE, '>', $outfile); print IMAGE $thumb->jpeg; close(IMAGE); } sub thumbnail_dimensions { my ($x, $y, $ratio, $height, $width); ($x, $y) = @_; $ratio = $x / $y; if ($default_orientation eq 'height') { $height = $default_height; $width = int($height * $ratio); } else { $width = $default_width; $height = int($width / $ratio); } return $width, $height; }

I made some tests over few images and I still prefer the thumbnails produced by ImageMagick.

Ciao, Valerio

Replies are listed 'Best First'.
Re: Re: Creating thumbnails
by cal (Beadle) on Oct 31, 2002 at 01:07 UTC
    Still being new to perl I am not clear where the values of these variables in both of these subs are coming from?
    sub thumb_with_gd my ($infile, $outfile, $image, $error, $x, $y, $width, $height, $thumb); sub thumbnail_dimensions my ($x, $y, $ratio, $height, $width);
    Thanks agian Cal

      As far as I can see... They get their values later on in the sub, $infile and $outfile from the values passed to the sub, $image becomes the new image created from $infile, $x and $y it's dimensions, $width and $height the new dimensions, and $thumb the new image. Guess $error is a leftover from the ImageMagick version?

      As for thumbnail_dimensions... $x and $y are passed to the sub, $ratio is their quotient, and $width and $height are the new dimensions. The mys just give them an undef value.

      Hope this helps. Sorry if I misunderstood you.

Re: Re: Creating thumbnails
by cal (Beadle) on Oct 30, 2002 at 23:37 UTC
    This is a great comparison script. I whish that I could use it but I cannot use Image::Magik on my server. Howver The GD.pm portion will be extremely helpful] Thanks so much for your help Cal