Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

GD thumbnail script errors

by silent11 (Vicar)
on Apr 17, 2002 at 19:26 UTC ( [id://159955]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to make jpeg image thumbnails with GD, not Image::Magick.
I found the following code below here, but I can't get it to work right
Sometimes I get errors, and sometimes it works, when it does work,
the thumbnail only slightly resmebles the source file.

What is going wrong?

-thanks in advance,
silent11
#! perl -w use GD; my $source = 'meat.jpg'; my $Thumbnail = 'thumb_' . "$source"; my $maxheight = 150; my $maxwidth = 150; my $srcimage = GD::Image->newFromJpeg("$source"); my ($srcW,$srcH) = $srcimage->getBounds(); my $wdiff = $srcW - $maxwidth; my $hdiff = $srcH - $maxheight; my $newH; my $newW; if ($wdiff > $hdiff) { $newW = $maxwidth; $aspect = ($newW/$srcW); $newH = int($srcH * $aspect); } else { $newH = $maxheight; $aspect = ($newH/$srcH); $newW = int($srcW * $aspect); } print "converting $srcW:$srcH to $newW:$newH\n"; my $newimage = new GD::Image($newW,$newH); $newimage->copyResized($srcimage,0,0,0,0,$newW,$newH,$srcW,$srcH); open(FILE, ">$Thumbnail") || die "Cannot open $Thumbnail : $!\n"; print FILE $newimage->jpeg;

Replies are listed 'Best First'.
(Guildenstern) Not using GD, but it works...
by Guildenstern (Deacon) on Apr 17, 2002 at 21:03 UTC
    I wrote a similar script to create thumbnails of images, but instead of using GD, I used some of Tk's functionality. Here's the relevant snippet:
    use Tk; use Tk::JPEG; use Image::Size; #..snip.. my $main = new MainWindow; my $canvar = $main->Canvas()->grid; #..snip.. # $im_format is either "gif" or "jpeg" # $_ is full path to file my $image = $canvar->Photo('-format' => $im_format, -file => $_); # Using the Image::Size module my ($x, $y) = imgsize($_); # An easy number for me to use - YMMV my $scale_factor = ceil($x / 100); # This will be the thumb my $newimg = $canvar->Photo(); # This is where the magic happens $newimg->copy($image,'-subsample' =>$scale_factor); # Write it to file $newimg->write($thumb_name, -format => $im_format); # Housekeeping $image->delete(); $newimg->delete(); #..snip..


    Not super elegant, but it works!

    Guildenstern
    Negaterd character class uber alles!
Re: GD thumbnail script errors
by fireartist (Chaplain) on Apr 17, 2002 at 23:05 UTC
    I've never used GD, so I won't comment on your code, but I do know my image editing.
    If your script isn't outputting any errors, and the final image does "slightly resemble the source file", it may be a problem in the way you're resizing, rather than any errors in the code you're using.

    I'll explain:
    If the original image is significantly larger than the final thumbnail, then you may need to resize it in two or three steps, to maintain image quality. I personally don't like to reduce an image by more than 25% in one pass, particularly if the final image is very small (and 150x150 is quite small).

    I can't provide GD specific code for this, but what I suggest you could do is something like this,
    (Please note, this is not real code, this is to explain the theory to you)
    open image; get image_width; if (image_width > (150 * 4)) { change_image_size to "25%"; } change_image_size to final_thumbnail_size; print_image;
Re: GD thumbnail script errors
by blogan (Monk) on Apr 17, 2002 at 22:22 UTC
    Make sure you close the file, otherwise you might not have the buffers flushed. I don't know for sure if Perl does this automatically for you or not.
Re: GD thumbnail script errors
by silent11 (Vicar) on Apr 17, 2002 at 21:23 UTC
    I can't edit my node for some reason, so I'll update it here.
    I forgot to set binmode() after opening my file, and before printing to it. like this :
    binmode (FILE);
    -Silent11
Re: GD thumbnail script errors
by mattr (Curate) on Apr 18, 2002 at 14:00 UTC
    Not completely sure, but the code at the link you mentioned has at least two typos. unless the same method is flipping a dice to decide whether it prints a jpeg or a png.. and chicken has only one c. So I wouldn't trust it to be correct.

    I've had good results with gimp and perl-fu which I found does high-quality resizing.

    You might like to see if you can properly load the jpeg and save it to a new file without resizing it first. Conceivably GD can't read someone's implementation.

Re: GD thumbnail script errors
by dmmiller2k (Chaplain) on Apr 20, 2002 at 12:07 UTC
    For what it's worth, merlyn wrote a column in WebTechniques addressing the issue of thumbnails, at Thumbnails with PBMTOOLS (Sep 98). Although he doesn't use GD, perhaps the structure of the code will offer you some assistance.

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://159955]
Front-paged by dsb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-23 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found