graq has asked for the wisdom of the Perl Monks concerning the following question:
I am (was) having some (inconsistent) problems with Image::Magick and creating new sizes of images.
My main problem today, however, is that the code has started working correctly, without any errors. When I left last night, the script below was creating 4 files: 'image_original.bmp', 'image_main.bmp', 'image_thumb.bmp-0' and 'image_thumb.bmp-1'.
Having created this script to illustrate my problems, I was about to post it on here asking: "Why is it creating 2 thumb nails?" - but now I would also like to know "Why is it not?".
#!/usr/bin/perl use strict; use warnings; use Image::Magick; use Image::Info qw(image_info); use IO::File; use CGI qw(:standard); use File::Basename; use Data::Dumper; unless( param('submit') ) { print <<HTML; Content-type: text/html <html> <head><title>Image Magick Resize / Copy Test</title></head> <body> <form name="form1" method="POST" enctype="multipart/form-data"> <input type="file" name="image_file" /> <input type="submit" name="submit" value="submit"/> </form> </body> </html> HTML exit; } my $filename = param('image_file'); my $cgi = new CGI; my $fh = $cgi->upload('image_file'); my $outfile = new IO::File; my $buffer; # open the pipe to the output file open ($outfile,">/tmp/someimage") or die( "Could not write file: $!" ) +; while (my $bytesread=read($fh, $buffer, 1024)) { print $outfile $buffer; } close $outfile; my $info = image_info("/tmp/someimage"); my $extension = $info->{file_ext}; # now create a file with the proper extension, and zap the original rename( "/tmp/someimage", "/tmp/image_original".".$extension"); { my $oImageMagick = Image::Magick->new; $oImageMagick->Read("/tmp/image_original".".$extension"); my $img_width = $oImageMagick->Get('columns'); my $img_height = $oImageMagick->Get('rows'); my $longest = $img_width>=$img_height? $img_width:$img_height; my $ratio_main = 600 / $longest; $oImageMagick->Resize(width=>$img_width * $ratio_main, height=>$img_ +height * $ratio_main); $oImageMagick->Write('/tmp/image_main'.".$extension"); } { my $oImageMagick = Image::Magick->new; $oImageMagick->Read("/tmp/image_original".".$extension"); my $img_width = $oImageMagick->Get('columns'); my $img_height = $oImageMagick->Get('rows'); my $longest = $img_width>=$img_height? $img_width:$img_height; my $ratio_thumb = 200 / $longest; $oImageMagick->Resize(width=>$img_width * $ratio_thumb, height=>$img +_height * $ratio_thumb); $oImageMagick->Write('/tmp/image_thumb'.".$extension"); } print "Content-type: text/html\n\n"; print "File uploaded\n";
-=( Graq )=-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Duplicate Images with Image::Magick Resize
by zentara (Cardinal) on Nov 14, 2006 at 14:25 UTC | |
by PerlGuy(Tom) (Acolyte) on Jul 20, 2009 at 16:27 UTC | |
by zentara (Cardinal) on Jul 21, 2009 at 09:45 UTC | |
by ELISHEVA (Prior) on Jul 21, 2009 at 09:57 UTC |