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

Learning is fun! You do fall off your surfboard a lot, though. I'm working on an image upload and resize CGI, and here's where I'm (look for the line with the obscene number of #s for where I'm having trouble):
#!C:\Perl\bin\perl.exe -wT ## use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; use File::Basename; $CGI::POST_MAX = 1024*5000; $CGI::DISABLE_UPLOADS = 0; my $query = CGI->new; my $safeCharacters = 'a-zA-Z0-9_.-'; my $uploadDirectory = 'C:/Apache/htdocs/notoriousteaze.com/Images/Temp +'; my $originalImage = $query->param("Image"); my ($filename, undef, $ext) = fileparse($originalImage, qr(\..*)); $filename .= $ext; $filename =~ tr/ /_/; $filename =~ s/[^$safeCharacters]//g; if ($filename =~ /^([$safeCharacters]+)$/) { $filename = $1; } else { error("That file name doesnt work for me. $filename"); } my $uploadFilename = $query->upload("Image"); open(UPLOADFILE, ">$uploadDirectory/$filename") or error('Could Not Up +load File.'); while ( <$uploadFilename> ) { print UPLOADFILE; } close UPLOADFILE; my %resizeFileType = ( ".jpeg" => \&resizeJpeg, ".jpg" => \&resizeJpeg, ".gif" => \&resizeGif, ".png" => \&resizePng, ); if(defined ($resizeFileType{$ext})) { $resizeFileType{$ext}->($uploadDirectory, $filename); } else { error("Not seeing the filetype: $ext"); } sub error { my $error = shift(); print $query->header(), $query->start_html(-title=>'Error'), $error, $query->end_html; exit(0); } sub resizeJpeg($$) { use GD; my $newDirectory = 'C:/Apache/htdocs/notoriousteaze.com/Images/News' +; my $thumbDirectory = 'C:/Apache/htdocs/notoriousteaze.com/Images/New +sThumbs'; my $source = "$uploadDirectory/$filename"; my $target = "$newDirectory/$filename"; my $thumbTarget = "$thumbDirectory/$filename"; my $targetMaxX = 250; my $targetMaxY = 250; my $thumbMaxX = 80; my $thumbMaxY = 80; ##$source = shift(); ##$target = shift(); ##$thumbTarget = shift(); GD::Image->trueColor(1) or error("$source, filename: $filename"); ## +################This is where it seems to be conking out on me!!##### +############################################################ $buildable = newFromJpeg GD::Image($source, 1) or error('Could not c +reate Image.'); (my $width, my $height) = $buildable=> getbounds(); if ($width > $height) { $targetMaxY = $height / ($width / $targetMaxX); $thumbMaxY = $height / ($width / $thumbMaxX); } else { $targetMaxX = $width / ($height / $targetMaxY); $thumbMaxX = $width / ($height / $thumbMaxY); } my $resizedImage = newTrueColor GD::Image($targetMaxX, $targetMaxY); my $newThumb = newTrueColor GD::Image($thumbMaxX, $thumbMaxY); $resizedImage->copyResized($buildable, 0, 0, 0, 0, $targetMaxX, $tar +getMaxY, $width, $height); $resizedImage->interlaced('true'); $newThumb->copyResized($buildable, 0, 0, 0, 0, $thumbMaxX, $thumbMax +Y, $width, $height); $newThumb->interlaced('true'); open(DATEI, ">$target") or error('Could not write to target file.'); while ( <$resizedImage> ) { print DATEI; } close DATEI; open(DATEI, ">$thumbTarget") or error('Could not write to thumb file +.'); while ( <$resizedImage> ) { print DATEI; } close DATEI; } sub resizeGif { } sub resizePNG { }
I'm trying to get this ironed out to work with the Jpegs first, then I'm hoping it'll be pretty straightforward to get other formats working as well. The current problem seems to be at line 87. GD::Image -> trueColor(1) sends me to the error message. I've tried doing a bit of monkeying around with it, but to no avail so far. I'm a novice yet, so be gentle :-). Cheers, K.

Replies are listed 'Best First'.
Re: GD::Image trueColor() hangup...
by Joost (Canon) on Mar 26, 2008 at 21:13 UTC
      Joost, Is there a course of action you'd recommend, based on that? Just getting rid of the line gives me a Premature end of headers error when I run the script. Thank you for your help! Cheers, K.
Re: GD::Image trueColor() hangup...
by BrowserUk (Patriarch) on Mar 26, 2008 at 21:16 UTC

    Although one of the examples in the POD suggests that trueColor() returns an image, it doesn't. The second example is correct. Ie. No return. So ignore the return code.

    To verify it worked, call GD::IMage->trueColor(1);, then create your image, my $img = GD::Image->new( $x, $y ) or die ... and then call $img->isTrueColor() or die ....

    Or, just pass a third argument to the constructor my $img = GD::Image->new( $x, $y, 1 );. The same works for all the other constructors also.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: GD::Image trueColor() hangup...
by mr_mischief (Monsignor) on Mar 26, 2008 at 21:15 UTC
    The docs for GD say that GD::Image->trueColor(1) is for backwards compatibility and also that it needs to be done before creating new images.

    I have a feeling you'll want to ditch that option altogether and specify what type of new image you want with something like:

    my $blank_rgb_image = GD::Image->newTrueColor( $width, $height );

    or

    my $blank_palette_image = GD::Image->newPalette( $width, $height );
    explicitly.

    I've never tried to get the package-wide palette/truecolor option to work in new code, since GD.pm has offered specific methods for both for quite a while now.

    You'll probably also want to get rid of the indirect object syntax in your code. It my not bite you now, but it's better to get into a habit of using direct syntax and not worry about it. In case you're wondering what that means, change for example:

    my $foo = new GD::Image;
    to
    my $foo = GD::Image-new;
    which does not suffer from the type of ambiguous parsing of the former.
      Hi Mr. M, Thanks for the response... I've gotten rid of the indirect syntax (at least I think I've gotten rid of all of it), which certainly is aesthetically pleasing to me :-). However, I'm still not able to get the script working. I'm all for creating the image without bringing trueColor() into the mix separately. But in order to know the height and width of the image, I need to be able to getbounds() on the source image. The code I've pieced together does this after making $buildable. Is there another way to get the height and width of the original image? Cheers, K.
        I don't see any problem with calling getbounds() where you do. The only problems I see are that you've got '=>' instead of '->' and that there's a space between that and the name of the method.

        IOW, change $buildable=> getbounds() to $buildable->getbounds().

        You might also want to change from  (my $width, my $height) to my ( $width, $height ) on that line. The syntax you're using gets the job done, but it's not the way you'll see most people writing that.