Image::Magick and perl script STDOUT problem

R

rbonin

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
 
A

A. Sinan Unur

(e-mail address removed) wrote in
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
able read files from your visitors' hard drives, and all your visitors
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
 
R

rbonin

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
easy to read.

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?
 
S

samwyse

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.

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. said:
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.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @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
 
S

Sherm Pendley

A. Sinan Unur said:
(e-mail address removed) wrote in
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--
 
S

Sherm Pendley

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::print(), 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--
 
A

A. Sinan Unur

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
 
R

rbonin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top