Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Duplicate Images with Image::Magick Resize

by graq (Curate)
on Nov 14, 2006 at 08:36 UTC ( [id://583926]=perlquestion: print w/replies, xml ) Need Help??

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 (Archbishop) on Nov 14, 2006 at 14:25 UTC
    If you can't reproduce the error, and if it was "inconsistent", then you have to assume it had something to do with the way the server was running it. Maybe server load? Maybe you had a different test script running last night, and this morning with a rested mind, you have the right script running? Maybe you ran the cgi script twice, before the /tmp/directory was cleaned, and IM was trying not to overwrite a file, by automatically renaming it? Your script runs fine here.

    I run into the same thing when I code on a Sunday..... scripts just start running weird....then on Monday they work fine. ???? I can only conclude that I was mis-hitting keys and was cross-eyed on Sunday, and too tired to realize it.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      I've had, over the years, several such perplexing instances where a newly written (or written, rewritten and rewritten...) script, especially when attempting to do something new and unusual, and where the code might be interpreted somewhat ambiguously, refused to work. No way no how...

      Then after being up half the night (or several) and giving up and going to bed would find that upon returning to work in the morning the script would just work.

      My conclusion, after experiencing this several times over the years is that Perl, at first, couldn't quite understand what I wanted. There was some kind of communication block due to the fact that I was asking for something that hadn't been done before.

      Then - during the night, something clicked and Perl said "a hah!" realized what I wanted... picking this up from my subconscious mind while I was sleeping and wanting to please me... surprised me by working "correctly" in the morning.

      In other words, there seems to be something sentient (intangible/mystical/spiritual) at the heart of Perl. Or perhaps it was simply force of will. At any rate, I was never able to figure out anything that I could have done materially or physically to cause the program(s) to suddenly start running differently (as intended rather than some other way or not at all) after "sleeping on it".

        I was discussing something similar to this with a woman friend of mine. She complained how a man will often say " there is nothing wrong with this ( piece of machinery, or whatever ), I'll be back tommorrow. The man, will then go into this state of mind where he thinks his "luck, karma, or magic" will somehow alter physical reality, and make the thing work the next day.

        I have often experienced this sort of magic, but it usually takes something extra....like sending out good thoughts, good preventative maintenance, or prayers to the gods. ( no chicken sacrifices here :-) ).

        Anyways, Perl and the way computers run, are very machine-like; and it is very unlikely there is any magic in the way things run , or don't run. It is more like some sort of hardware issue.....like overheating slowing things down, and it runs better the next morning when its cool. Or, you were erroneously running an older buggy version of the script due to some other software error.( maybe thru an IDE? , always use Midnight Commander :-) )

        However, if you can scientifically reproduce your "psychic debugging technique", where you can fix code by dreaming on it.....you will be in great demand!!

        Of course, dreaming of a bug-fix, then implementing the next day by changing the code, is real..... I do it all the time....... and I wonder who it is in "dream-space" that is acting as my muse. :-)

        Of course, the "event-stream" of real life is pure magic, so you may very well be experiencing something out of the ordinary...... but you will have a hard time proving it......its done that way to protect the innocent......


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku

        When you stare at a problem over and over it is easy to fall into an unconscious rut, making the same subtle mistake over and over. It could be something so simple as running the program in the wrong directory or being oblivious to the fact that an environment variable was set 6 hours ago. (Or my favorite bug-a-bears: forgetting to save before I run or repeatedly running a different file from the one I'm editing.)

        When you get up the next morning, refreshed and clear-eyed (well, maybe when you get up and have a first cup of caffine), you stop making the same mistake. Or maybe you changed the environment by logging in and out.

        zentara is right. The computer and the software that runs on it are deterministic. Something has changed. If it isn't the code then it is either the environment or you.

        Best, beth

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://583926]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found