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

How can I resize an uploaded image using the CGI upload function? I have had no problems with uploaded images... I can saved them/output them, etc just fine. But when I try to import it into GD, I either get an error that the argument is not numeric, or I get an error on copyresampled() that I am using it on an undefined value. Any examples would be appreciated!

Replies are listed 'Best First'.
Re: Resizing Uploaded Images?
by hardburn (Abbot) on Jun 10, 2003 at 17:56 UTC

    Try posting the bit of code in qustion along with the exact error message it generates.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I've tried a bunch of different variations trying to get this to work(including saving it to a temp file, then telling GD what that temp file is). This is the current code:

      #!E:\perl\bin\perl.exe -w
      
      use CGI qw(:standard);
      $savedir = "E:\\network\\apache2\\htdocs";
      $query = new CGI;
      
      use GD;
      
      if ($file=param('filename')) {
        save_file();
      } else {
        show_form();
      }
      
      sub show_form {
      print $query->header();
      print $query->start_html("File Uploading"),
            h1('File uploading'),
            $query->start_multipart_form(),
            p,
            "The file to upload:",
            p,
            $query->filefield(-name=>'filename',
                              -size=>45),
            p,
            $query->submit(-name=>'submit',
                           -value=>'Upload File'),
            $query->endform,
            $query->end_html;
      }
      
      sub save_file {
        $file2 = upload('filename');
      
        $im = new GD::Image($file2);
        $rim = new GD::Image(240,240);
      
        ($width,$height) = $im->getBounds();
        $rim->copyResized($im,0,0,0,0,$width,$height,240,240);
      #close FILE;
      
      #  print "Content-type: image/png\n\n";
        print "Content-type: text/html\n\n";
      
      #  binmode STDOUT;
        print "$width $height";
      #  print $rim->png;
      }


      I am printing the width and height to at least see if the image is being imported; but it obviously is not. The errors I get:

      Tue Jun 10 15:46:23 2003 error client 192.168.200.3 Name "main::savedir" used only once: possible typo at E:/network/Apache2/cgi-bin/upload.pl line 4., referer: http://cir/cgi-bin/upload.pl
      Tue Jun 10 15:46:23 2003 error client 192.168.200.3 Name "main::file" used only once: possible typo at E:/network/Apache2/cgi-bin/upload.pl line 9., referer: http://cir/cgi-bin/upload.pl
      Tue Jun 10 15:46:23 2003 error client 192.168.200.3 Argument "E:\\network\\Apache2\\htdocs\\file.jpg" isn't numeric in subroutine entry at E:/network/Apache2/cgi-bin/upload.pl line 35., referer: http://cir/cgi-bin/upload.pl

      I really would like to use something other than GD, but I have no choice.

      Thanks for your help!
        Oops, sorry. I forgot to login when I posted that.
Re: Resizing Uploaded Images?
by glwtta (Hermit) on Jun 10, 2003 at 20:12 UTC
    GD is fairly horrible for scaling images (great for generating them as it is), give Imager a try; while it won't solve your current problem (which we can't really say anything about without seeing the code), it will give you an allaround more pleasant time manipulating images.

    Supposedly Image::Magick is good for this sort of thing too, but personally I haven't been able to get it to install (and didn't have time to wrestle with it), Imager, on the other hand, needs no bulky external libraries.

Re: Resizing Uploaded Images?
by erasei (Pilgrim) on Jun 10, 2003 at 18:57 UTC
    I was in the same boat you are, a few months back. I write Perl for a living, and I'm not entirely bad at it but I had a terrible time with GD. Honestly, I didn't really give it a lot of effort, because while looking around the net for help, I found ImageMagick.org.

    They provide some excellent Linux software (maybe other platforms too) with C and Perl APIs. Highly recommended as their software can do just about anything with an image.

Re: Resizing Uploaded Images?
by Hagbone (Monk) on Jun 11, 2003 at 01:27 UTC
    I claim no expertise whatsoever here, but I will relate my experience with similar pursuits ....

    If you are interested in resizing images for display in a web page (and after they are uploaded), my experience has been .....

    You can use "dumbnails" to resize without actually "touching" the original image. By calling the image via HTML, and using proportional width="whatever" and height="whatever" values, you can reduce the size visually of the image (using the Image::Size module), but you don't change a damn thing regarding the original (possibly bloated) uploaded image. The downside to this approach is that every time someone loads a page with one of these "dumbnails", the server has to handle to entire, possibly bloated, image.

    I've also used ImageMagick to resize into true thumbnail's (and reduce the weight) of images on-the-fly, but the problem I've encountered has to do with the need to create temporary images to enable this method. The wall I hit was that you can create an image on the fly without creating a temporary image, but you can't include HTML with the output, cause of the need to call two different "Content-type:......" headers. Creating temp images works, but geez, it means a lot of create and destroy type programming that seems kind of counter productive to my feeble mind.

    So ... I've got one area where I use dumbnails .... and I'll probably leave it like that, 'cause the situation involves only one image at a time, and I have image weight constraints installed in the upload programming so I can prevent "server-busting" images from being uploaded. I also have added a link below the dumbnail that points to the "view full size image". The good news is that once the dumbnail loads, viewing the full size image is damn near immediate 'cause most browsers keep the image in cache.

    I've got another area where I'm displaying a dozen or more thumbnails, and in this area, I use the "create-a-temp-and-then-destroy" method.

    I doubt this really offers any answers or insight, but I've posted the above in the hopes that it might help you, and also in the hopes that it might help me when some much-smarter-person replies with a comment that points out the stupidity in the way I'm handling image manipulation (and hopefully includes a neat and tidy solution to the issues I've brought up).