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

Hello Monks, After viewing many posts about creating thumbnail images I am clear that the only sensible methods to enable the use of Image::Magic is:

1.To own my own server.
2 Request the current host to install it.
3.Go to another host that already supports it.

Since the only real drawback to using the GD.pm module seems to be image quality for the most part. This might be an option.

Still being new to perl I always want to know how involved is the code to accomplish creating thumbnails as they are uploaded to a website or is there substantial code examples using this module for this application.

If there is can someone please direct me or give a brief code example.
Thanks,
Cal

Replies are listed 'Best First'.
Re: Creating thumbnails
by valdez (Monsignor) on Oct 30, 2002 at 22:43 UTC

    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

      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.

      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
Re: Creating thumbnails
by abell (Chaplain) on Oct 30, 2002 at 21:32 UTC
    In one of my sites I have been using the following function for a while (uses Image::Magick):
    use Carp; use Image::Magick; ... # makethumbnail( jpeg-data ) # creates a thumbnail with maximum size 120x120 sub makethumbnail { my $data = shift; my $image=Image::Magick->new(magick=>'JPEG'); my $res; $res = $image->BlobToImage($data); if ($res) { carp("makethumbnail: Error reading image - $res\n"); return undef; } $res = $image->Transform(geometry=>'120x120'); if ($res) { $res = $image->Resize(geometry=>'120x120'); if ($res) { carp("makethumbnail: Error while resizing: $res\n"); return undef; } } my @blob = $image->ImageToBlob(); if ($#blob!=0) { carp("makethumbnail: Error while converting to blob\n"); return undef; } return $blob[0]; }

    I seem to recall that the double command for resizing depends on the interface changing at some point.

    Cheers
    Antonio

    Update: I hadn't read the original post carefully enough. Of course you were asking for a non-Image::Magick solution. Hopefully someone will find this helpful anyway.

    The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket
Re: (nrd) Creating thumbnails
by newrisedesigns (Curate) on Oct 30, 2002 at 21:18 UTC

    Maybe I'm misinterpretting, but why would you want to make thumbnails on your server? Why not make them using Perl & Image::Magick on your local machine and upload them from there?

    If this doesn't help, please explain your situation in greater detail so your fellow Monks can help you.

    John J Reiser
    newrisedesigns.com

      Thanks, I guess the problem is not being able to get the Image::Magik module loaded on my remote server. The web hosting company feels the module is a security threat.