Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Just another Perl shrine

Image::Magick and outputting to browser

by wil (Priest)
on May 21, 2003 at 14:27 UTC ( #259732=perlquestion: print w/ replies, xml ) Need Help??
wil has asked for the wisdom of the Perl Monks concerning the following question:


I'm having some problems outputting the results of Image::Magick to a web browser. I can output to a file and it works just fine, but for some reason I get the following error when trying to output to a browser window. My code follows.

my $self = shift; my $q = $self->query(); my $image_path = $q->param('image'); $self->header_props ( -type =>'image/png', ); my ($image,$x); my $image = Image::Magick->new(); $x = $image->Read(filename =>"/images/users/$image_path"); $x = $image->Resize(geometry =>'175x275'); # THIS DOES NOT WORK - WHY? binmode STDOUT; $x = $image->Write(.png:-'); # THIS WORKS PERFECTLY: # $x = $image->Write(filename =>'/images/users/resize/test.png');
The image “http://phoenix/;image=481_img-home2.j +pg” cannot be displayed, because it contains errors.

- wil

Comment on Image::Magick and outputting to browser
Select or Download Code
Re: Image::Magick and outputting to browser
by arthas (Hermit) on May 21, 2003 at 14:37 UTC
    You probably need to specify the content-type of what you are sending to the browser. Something like the following should do:
    print "Content-type: image/png\n\n"; binmode STDOUT; $x = $image->Write(.png:-');
•Re: Image::Magick and outputting to browser
by merlyn (Sage) on May 21, 2003 at 19:24 UTC
    When PerlMagick is writing to standard output, it does so without respecting the buffered data in Perl's buffer. Either unbuffer STDOUT, or make sure it's flushed before calling the ImageMagick routine.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Image::Magick and outputting to browser
by glwtta (Hermit) on May 21, 2003 at 20:13 UTC
    A small aside - judging by those paths you are on some sort of UNIX-like system, I was under the impression that binmode is only necessary on Windows - have I been misled?

      yes, binmode does nothing on systems that don't distinguish between binary and text files. however, it's good practice to include it because that'll be one less thing to have to worry about if you ever decide to port the code to windows.

      anders pearson

Re: Image::Magick and outputting to browser
by Hagbone (Monk) on May 21, 2003 at 22:17 UTC
    Interesting ... this is a problem I had as well, and after looking this thread over, I've solved my problem ... which I think is the same (or very much similar) : not being able to print images on the fly, but having to write to a file first. Here's what I gleaned from the responses that allowed my script to work: As Michele mentions, you need to preface your lines:
    binmode STDOUT; $x = $image->Write(.png:-');
    print "Content-type: image/png\n\n";
    And as merlyn mentioned, you must insert:
    $| = 1;
    so the buffer is turned off .... this buffer switch was the core of my problem, and it appears to me (and make note, I'm positioned early in the ImageMagick learning curve), that your problem is two fold ... lack of proper header and failure to turn of buffering. The code below represents a copy of what is now working for me (on a Linux box)
    #!/usr/local/bin/perl use Image::Magick; ## turn off buffer .... $| = 1; my $src = Image::Magick->new; $src->Read('bird.gif'); # Create the thumbnail, where the biggest side is 50 px my ($thumb,$x,$y) = create($src,150); print "Content-type: image/gif\n\n"; binmode STDOUT; $thumb->Write('gif:-'); exit; sub create { my ($img,$n) = (shift,shift); my ($ox,$oy) = $img->Get('width','height'); my $r = $ox>$oy ? $ox / $n : $oy / $n; $img->Resize(width=>$ox/$r,height=>$oy/$r); return $img, sprintf("%.0f",$ox/$r), sprintf("%.0f",$oy/$r); }
    Hope this helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://259732]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-04-17 10:26 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (444 votes), past polls