# Image::Magick and perl script STDOUT problem

Discussion in 'Perl Misc' started by rbonin@gmail.com, Jul 20, 2005.

1. ### Guest

The image magick method Write('gif:-') is supposed to write the binary
file to STDOUT. I tried adding "binmode STDOUT" before the
$image->Write command but that did not help. The STDOUT from$image->Write('gif:-'); is not making it's way to the browser, the
browser is displaying a broken image. Looking at the raw response, I
see the correct header, just no data being sent.

Can I capture the STDOUT into a variable, and use a binary write to the
browser? I don't want to write the image to the hard drive.

<% @LANGUAGE="PerlScript" %>
<%
use Image::Magick;

my $image = Image::Magick->new;$image->Read('W:\\in.gif');

$Response->{ContentType} = "image/GIF";$image->Write('gif:-');

undef $image; %> Thanks Robin , Jul 20, 2005 1. ### Advertisements 2. ### A. Sinan UnurGuest wrote in news:: > The image magick method Write('gif:-') is supposed to write the binary > file to STDOUT. I tried adding "binmode STDOUT" before the >$image->Write command but that did not help. The STDOUT from
> $image->Write('gif:-'); is not making it's way to the browser, the > browser is displaying a broken image. Looking at the raw response, I > see the correct header, just no data being sent. > > Can I capture the STDOUT into a variable, and use a binary write to > the browser? I don't want to write the image to the hard drive. > > > <% @LANGUAGE="PerlScript" %> > <% > use Image::Magick; > > my$image = Image::Magick->new;
> $image->Read('W:\\in.gif'); > >$Response->{ContentType} = "image/GIF";
> $image->Write('gif:-'); > > undef$image;
>
> %>

None of your questions make any sense, because PerlScript is not Perl. For
the above to work, in addition to all sorts of things I do not know
anything about, PerlScript, running in Internet Explorer, would need to be
would need to have a W:\in.gif file on their computers.

Add to this the fact that there is no mime type image/GIF.

Add to this the fact that there is absolutely no reason whatsoever to use
Image::Magick here.

Something like the following ought to work as a CGI script:

#!/usr/bin/perl

use strict;
use warnings;

use constant CHUNK_SIZE => 4096;

open my $gif, '<', 'image.gif' or die "Cannot open image.gif:$!";

print "Content-Type: image/gif\015\012\015\012";

select STDOUT;
$| = 1; binmode STDOUT; while ( 1 ) { my$r = sysread $gif, my$buffer, CHUNK_SIZE;
die "File read error: $!" unless defined$r;
last unless $r; print STDOUT$buffer;
}

close $gif or die "Cannot close image.gif:$!";

__END__

> Thanks
> Robin

Hmmm ... Can't be, can it? Hope not.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

A. Sinan Unur, Jul 20, 2005

3. ### Guest

I'm using active state perl running from of an asp application. The
script is being executed server side. I unsderstand in my example their
is no reason to be using imagemagick. I'm doing more complex things I
just simplify the code for the purpose of posting it here and making it

> $image->Write('gif:-'); is not making it's way to the browser, the > browser is displaying a broken image. Looking at the raw response, I > see the correct header, just no data being sent. > > Can I capture the STDOUT into a variable, and use a binary write to the > browser? I don't want to write the image to the hard drive. Just by coincidence, the following post also appeared recently ("Tk: Text works with TIE, Scrolled doesn't", 7/14/2005 2:19 PM): Stefan H. <> wrote: > the following code works properly, printing on Text all I print to > STDOUT > > my$text = $mw->Text->pack(); > tie *STDOUT, ref$text, $text; > print "prova"; I hope that answers your question. samwyse, Jul 20, 2005 5. ### A. Sinan UnurGuest wrote in news:1121889144.699033.271690 @z14g2000cwz.googlegroups.com: [ Please quote an appropriate amount of context when you post a reply ] > My question is... can I catch the ouput of "$image->Write('gif:-');"
> that is supposed to write the binary data to STDOUT, and then write
> that data to the users browser?

I do not know why you would want to do that (I do not know anything
about IIS or ASP, so maybe there is some good reason).

But if you did want to get to the binary data, trying to capture the
output of $image->Write is still not the right way to do it. Use the ImageToBlob method of the Image::Magick object you have. The following works for me: #!/usr/bin/perl use strict; use warnings; use Image::Magick; my$img = Image::Magick->new;

{{
my $x =$img->Read('test.gif');
die "$x" if "$x";
}}

my $gif =$img->ImageToBlob;

binmode STDOUT;
print $gif; __END__ I suspect you have a web server configuration issue rather than a Perl issue. Sinan -- A. Sinan Unur <> (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html A. Sinan Unur, Jul 20, 2005 6. ### Sherm PendleyGuest "A. Sinan Unur" <> writes: > wrote in > news:: > >> <% @LANGUAGE="PerlScript" %> > None of your questions make any sense, because PerlScript is not Perl. Actually, it is Perl. ActiveState's distribution includes a WSH (Windows Scripting Host) plugin that allows Perl to be used in any app that uses WSH. That includes both IE and IIS. > the above to work, in addition to all sorts of things I do not know > anything about, PerlScript, running in Internet Explorer It wouldn't necessarily have to run in IE. It can be used server-side in ASP pages. > Add to this the fact that there is no mime type image/GIF. I thought MIME types were case-insensitive? sherm-- -- Cocoa programming in Perl: http://camelbones.sourceforge.net Hire me! My resume: http://www.dot-app.org Sherm Pendley, Jul 20, 2005 7. ### Sherm PendleyGuest writes: > The image magick method Write('gif:-') is supposed to write the binary > file to STDOUT. I tried adding "binmode STDOUT" before the >$image->Write command but that did not help. The STDOUT from
> \$image->Write('gif:-'); is not making it's way to the browser, the
> browser is displaying a broken image. Looking at the raw response, I
> see the correct header, just no data being sent.

Like A. Sinan said, there's an ImageToBlob method that's probably a better
way to get what you want.

It sounds to me like ActiveState's ISAPI plugin overrides Perl's print()
function to capture output - it *has* to be doing some sort of magic along
those lines, since it's running your Perl in-process rather than as a child
process, similar to how mod_perl runs it.

But ImageMagick is a C library; if the Write method is implemented in C, not
just a convenience wrapper for Perl, then it's not using Perl's print() for
its output. So the print() magic isn't working.

The details could be different - it could be using a tied file handle instead
of overriding main:rint(), for instance. Or something else. But the general
idea is the same regardless; if the low-level C library is doing its own I/O,
that's probably bypassing whatever magic handles Perl's print().

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Sherm Pendley, Jul 20, 2005
8. ### A. Sinan UnurGuest

Sherm Pendley <> wrote in news:m24qap5eo6.fsf@Sherm-
Pendleys-Computer.local:

[ snip all good points ]

Apologies for posting a message consisting mostly of wrong statements.

I should have looked in the eg subdirectory of my Perl installation first.

Sinan
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

A. Sinan Unur, Jul 20, 2005
9. ### Ron SavageGuest

Ron Savage, Jul 21, 2005
10. ### Guest

Thanks so much for all the help. The post ImageToBlob is what I was
looking for, and this last post by keith did everything I was looking
for.

Thanks
Robin

, Jul 21, 2005