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 )=-


In reply to Duplicate Images with Image::Magick Resize by graq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.