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

Quick question for the gurus who have experience with the GD.pm module. I am writing a routine to copy and scale images to thumbnail size. It works when I test locally but when I move to the webserver and test as a CGI it dies upon trying to call the copyResized method. I naturally want to understand better why it's failing so would like some error text from the module. One of the other method calls is documented to set $@ with error text upon an error, so I tried that but no such luck. Normally Lincoln Stein's documentation is damn-near flawless, but in this case doesn't give me a clue, and since the GD.pm module is a wrapper for the C-based gd library, I haven't the ability to look at the source to find out for myself.

Can anyone point me in the right direction? Thanks in advance. (Can post code if necessary but since this is a fairly general question and already a lengthy post, I thought I'd leave it at that.)

..Guv

Replies are listed 'Best First'.
Re: Retrieving error messages from the GD.pm module.
by theguvnor (Chaplain) on Mar 04, 2002 at 01:55 UTC

    OK per fuzzyping's request, here is the code (with only some names changed to protect the innocent ;) It is not my finest work as it's just my first run at it and I hard-code a lot of variables to a single "safe" case for testing. That said, if anyone does care to point out anything for improvement, be my guest! (Now that I've posted this it does look kinda ugly or at least verbose!!! :)

    Update:Thanks for a prod from Kanji, I tried just wrapping the call to copyResize() in an eval block and now everything works!!! I am curious if anyone knows about this method though; is it meant to be called in void context i.e. no return value? I was assuming a return of false/undef would indicate an error... it doesn't appear to be that way to my eyes.

    #! /usr/bin/perl -w use strict; use warnings; use diagnostics; use CGI; use CGI::Carp qw(fatalsToBrowser); # only for testing - rem +ove for production!!!!! use GD; # variables: my $q = CGI->new(); my ($srcImage, $tgtImage); # source and target imag +e object variables my ($srcext, $srcname); my $tgtfile; my ($maxw, $maxh) = (120, 120); # maximum width and hei +ght of new thumbnail my ($neww, $newh); # will be the actual wid +th and height of new thumbnail my ($width, $height); my $output; my $dir = '/home/mysite/private/upload'; # base directory f +or uploads my $id = 1; # hardcode one example for now, to test my $file = 'Iceberg.jpg'; # hardcode one example for now, to test die "Bad file parameter!" if $file =~ m/^[^a-zA-Z0-9 ._-]$/i; $output .= $q->p( "Preparing to make thumbnail of $file..." ); my $srcfile = "$dir/$id/$file"; # ensure user has a thumbnail directory: unless ( -e "$dir/$id/thumb" && -d "$dir/$id/thumb" ) { $output .= $q->p( "Creating /thumb directory for thumbnails..." ); mkdir "$dir/$id/thumb" or die "Couldn't make thumbnail directory $ +!"; } # some filename parsing: $file =~ m/([^.]+)\.?(\w*)$/; ($srcname, $srcext) = ($1, $2); # file extension is las +t part of name # open a filehandle for the source image file: open (SRC, '<', $srcfile) or die "Sorry, couldn't open $srcfile: $!"; binmode SRC; # create a new image object based on the source file: if ( $srcext =~ m/jpe?g/i ) { $srcImage = newFromJpeg GD::Image(\*SRC) or die "Problem reading $ +srcfile: $!"; } elsif ( $srcext =~ m/gif/i ) { $srcImage = new GD::Image(\*SRC) or die "Problem reading $srcfile: + $!"; } else { $output .= $q->p( "Can only convert jpg and gif format" ); display($output); exit; } close SRC; # determine width and height of source image: ($width,$height) = $srcImage->getBounds(); if ($width <= $maxw && $height <= $maxh) { # no work to be done! display( $q->p("File is already thumbnail-sized!!!") ); exit; } # determine limiting scale factor: my $wf = $width / $maxw; my $hf = $height / $maxh; my $scale = $wf > $hf ? $wf : $hf; # determine our new target image's scaled width and height: $neww = round($width / $scale); $newh = round($height / $scale); $output .= $q->p( "w factor = $wf, h factor = $hf, we'll scale by a fa +ctor of $scale" ); $output .= $q->p( "new image width x height will be $neww x $newh" ); # create new target image: $tgtImage = new GD::Image($neww, $newh); # copy the source image to the new image in scale: eval { $tgtImage->copyResized($srcImage, # source image - a GD::Image obje +ct 0, # target x co-ordinate 0, # target y co-ordinate 0, # source x co-ordinate 0, # source y co-ordinate $neww, # target width $newh, # target height $width, # source width $height # source height )}; die $@ if $@; #was: or die "Attempt to copy source image $file failed! +"; # create a jpeg datastream representation of the new thumbnail image: my $jpegdata = $tgtImage->jpeg() or die "Couldn't create jpeg thumbnai +l from source!"; $output .= $q->p( "jpeg data stream created successfully" ); # write data to new file: $tgtfile = "$dir/$id/thumb/$srcname.jpg";# name of new thumbnail-sized + image open (OUT, '>', $tgtfile) or die "Sorry, couldn't open target file for + writing: $!"; binmode OUT; print OUT $jpegdata; close OUT; $output .= $q->p( "Seem to be successful in creating a thumbnail versi +on of $file!" ); display($output); exit; sub display { # in production there is a much better way of doing this my $stuff = shift; print $q->header(), $q->html($stuff); return; } sub round { my($number) = shift; return int($number + .5); }

    ..Guv

      According to Thomas Boutell's (author of the C-based GD library to which Lincoln Stein has provided the Perl interface) documentation, CopyResized() is indeed a void function, as are the other copying and resizing functions:

      void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)

      If you're having trouble with GD and can't find an answer in Lincoln's pod, look at Boutell's documentation. If you're still stumped, there's always the source...

      -- grummerX

        I knew that GD.pm was a front-end for the C-based GD lib but (a) didn't know where the documentation for Boutell's GD lib was and (b) wouldn't know how to read the C source code for it if I could find it*.

        GrummerX++ - thanks for confirming my suspicions about void context!!!

        ..Guv

        * Slight exaggeration; though I wouldn't have a hope of writing C code I am pretty sure I would be able to read it at a beginner level i.e. enough to tell that the function call is in void context :)

Re: Retrieving error messages from the GD.pm module.
by fuzzyping (Chaplain) on Mar 04, 2002 at 01:38 UTC
    Actually, it would be great if you could post your code. Even if the local uber-guru's don't need it, some of us newbies can learn by your example. By browsing your code (unless it's copied verbatim from a pod sample), I'm bound to learn more about the module while trying to troubleshoot your dilemma.

    -fuzzyping