in reply to Making jpg thumbnails

I fiddled a bit around with GD and came up with the following code (it is untested, since ActiveState does not have GD):
#!perl use strict; use warnings; use GD; my $filename = '/path/to/img/img.jpg'; my $outfilename = '/path/to/img/thumb/img.jpg'; my $image = new GD::Image $filename or die "Could not open '$filename' +: $!\n"; my ($width, $height) = $image->getBounds; my ($newwidth, $newheight); if (($width > 300) || ($height > 300)) { if ($width > $height) { ($newwidth, $newheight) = (300, int((300 * $height) / $width)); } else { ($newwidth, $newheight) = (int((300 * $width) / $height), 300); } } else { ($newwidth, $newheight) = ($width, $height); } my $newimg = $image->copyResized( $image, # source image 0, 0, # destination position 0, 0, # original position $newwidth, $newheight, $width, $height ); $newimg->(); open OUTFILE, '>', $outfilename or die "Failed to open '$outfilename' +for output: $!\n"; print OUTFILE $newimg->jpeg; close OUTFILE;
I hope it works and suits your needs.
Cheers, CombatSquirrel.

Update: For files not supplied by ActiveState, have a look at PPM::Repositories, thanks to PodMaster.
Entropy is the tendency of everything going to hell.

Replies are listed 'Best First'.
Re: Re: Making jpg thumbnails
by wirrwarr (Monk) on Aug 26, 2003 at 14:55 UTC
    When the constructor new doesn't work, you might want to try something like
    my $image = newFromJpeg GD::Image( $filename );
    (I know I had problems when trying to use new, but probably I was just too dumb.)

    daniel.
Re: Re: Making jpg thumbnails
by bradcathey (Prior) on Aug 30, 2003 at 14:38 UTC
    Many thanks to CombatSquirrel for getting me on the right path. Got it to work with the following edits, just in case anybody else stumbles across this thread and wants to try it:
    #!perl use strict; use warnings; use GD; my $filename = '/path/to/img/img.jpg'; my $outfilename = '/path/to/img/thumb/img.jpg'; my $image = new GD::Image->newFromJpeg($filename); #edited my ($width, $height) = $image->getBounds; my ($newwidth, $newheight); if (($width > 300) || ($height > 300)) { if ($width > $height) { ($newwidth, $newheight) = (150, int((150 * $height) / $width)); } else { ($newwidth, $newheight) = (int((150 * $width) / $height), 150); } } else { ($newwidth, $newheight) = ($width, $height); } my $newimg = new GD::Image($newwidth, $newheight); #edited $newimg->copyResized( $image, # source image 0, 0, # destination position 0, 0, # original position $newwidth, $newheight, $width, $height ); open (OUTFILE, ">$outfilename") or die "Failed to open '$outfilename' +for output: $!\n"; binmode(OUTFILE); #edited print OUTFILE $newimg->jpeg(100); #edited close OUTFILE;
    bradcathey