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

Hi Monks!
I am working on this code and before I upload the photos to the server I need to resize these pictures, I cant get to work. If anyone would look into this code and help me would be very nice!
#!/usr/bin/perl -w use strict; use CGI::Carp qw( fatalsToBrowser ); use CGI ':standard'; use Image::Size; use Image::Magick; my (@inx,$ac,$ar,@filename,@array_reference,@working_filename,$array_b +egin,$array_end,$in,$save_file); my $image = Image::Magick->new; my $upload_dir = "../../pics"; $CGI::POST_MAX = 2048; binmode(STDIN); @inx = <STDIN>; #my ($ac,$ar); $ac = 0; $ar = 0; foreach (@inx) { delete @inx[$ac] if $_ =~ /^Content-Type: application\/octet-stream +/; $ac++; } $ac = 0; # Define individual uploaded files within the array foreach (@inx) { if ($_ =~ /^Content-Type/) { $filename[$ar] = $inx[$ac-1]; $array_reference[$ar] = $ac; @working_filename = split /filename=/, $filename[$ar]; @working_filename = split /"/, $working_filename[1]; # " @working_filename = split /\\/, $working_filename[1]; $filename[$ar] = pop @working_filename; $ar++; } $ac++; } $ac = 0; $ar = 0; print "content-type: text/html\n\n"; # Pull files from the array and write to disk foreach (@filename) { $array_begin = $array_reference[$ac] + 2; $array_begin = 0 if $array_begin < 0; $array_end = $array_reference[$ac+1] - 3; $array_end = ($#inx) if $array_end < 0; for (my $ii = $array_begin; $ii <= $array_end; $ii++) { $in .= $inx[$ii]; } $in = substr($in,0,length($in) - 2); $save_file = "$upload_dir/$_"; # get the image size my( $width, $height ) = imgsize($save_file); # resize image files here $image->read('$save_file'); $image->Set( Gravity => 'Center' ); $image->Resize( geometry => '180x168' ); $image->Extent( geometry => '180x168' ); my $n_image = $image->Write( '$save_file' ); # get the new image size here and save the new file size to dir my( $width2, $height2 ) = imgsize($n_image); # I am printing here to make sure the photo has been resizedm but i +t is not working print "<br>2-$width2, $height2<br>"; open(FF,">$save_file") or die $!; binmode(FF); print FF $in; close(FF); $in = ""; $ac++; print "Uploaded file $save_file<br />"; print "<IMG src=\"$save_file\" width=\"180\" height=\"168\">"; }

Thanks!

Replies are listed 'Best First'.
Re: Uploading Multiple Photos and Resizing before saving Help!
by kcott (Archbishop) on Nov 16, 2010 at 02:49 UTC

    You really need to tell us what part you cant get to work. Problems with uploading? Problems with resizing? Are there error messages? What else can you tell us?

    Here's one part you can fix:

    foreach (@inx) { delete @inx[$ac] if $_ =~ /^Content-Type: application\/octet-stream +/; $ac++; }

    delete is intended to operate on hashes (or slices thereof). Note the documentation: "Be aware that calling delete on array values is deprecated and likely to be removed in a future version of Perl.". That piece of code could be written like:

    @inx = grep { $_ !~ /.../ } @inx;

    -- Ken

      Upload works, resizing the images is where the issue is. I need the photos resized from lets say 1600X1200 resized to 180x168 before they can get to its final destination/directory.
        There is no possible way upload works as you're attempting to do the job of the CGI module.
Re: Uploading Multiple Photos and Resizing before saving Help!
by aquarium (Curate) on Nov 16, 2010 at 02:51 UTC
    what isn't working? the re-sizing, saving, or printing of new dimension sizes? the code  print "<br>2-$width2, $height2<br>"; looks wrong, as perl will likely turn it into a math result of two minus the value of width2 variable. did you mean?
    print '<br />2-' . "$width2" . ',' . "$height2" . '<br />';
    the hardest line to type correctly is: stty erase ^H
      This print "<br>2-$width2, $height2<br>"; was just a test code it doesn't belong to the program. Forgot to removed it before posting, sorry!
        have you tried running it from command line with debug? for this you can either read the CGI doc on how to do it OR just temporarily mod the program and hardcode a filename in for processing. I assume the typical suspects (file permissions) are all ok for the CGI running in the webserver environment can write files to the directory?
        the hardest line to type correctly is: stty erase ^H
Re: Uploading Multiple Photos and Resizing before saving Help!
by JavaFan (Canon) on Nov 16, 2010 at 08:26 UTC
    $image->read('$save_file'); my $n_image = $image->Write( '$save_file' );
    Really? Your image is in a file whose first character is a dollar sign? You sure you don't want to drop the quotes?

    Perhaps you should check the return values of methods you're calling. I don't know the module, but I doubt the methods have meaningless return values.

Re: Uploading Multiple Photos and Resizing before saving Help!
by CountZero (Bishop) on Nov 16, 2010 at 10:41 UTC
    If the problem is in the resizing, perhaps you can have a look at modules such as Image::Magick::Thumbnail::Simple, Image::Magick::Thumbnail or even Image::Thumbnail if you need more flexibility.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      OK, you are saying that there is no way to resize these images using the approach that this code is trying? Has anyone here done something similar so I can see the code?
        Not at all.

        But why not use existing modules? I find no fun in re-inventing the wheel.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Uploading Multiple Photos and Resizing before saving Help!
by Anonymous Monk on Nov 16, 2010 at 10:16 UTC
    You have
    use CGI ':standard';
    But then you read from STDIN yourself and you don't use CGI. use CGI, its what you need to use.
      Yes that's a mistake, I actually need to change this to be a web base approach and get rid of "STDIN".
Re: Uploading Multiple Photos and Resizing before saving Help!
by aquarium (Curate) on Nov 16, 2010 at 22:16 UTC
    have you tried using the imagemagick binaries directly on the image(s) from command line?
    the hardest line to type correctly is: stty erase ^H