delivering image from a script

R

Richard Trahan

Gunnar said:
Read it and print it to STDOUT. You'll also need the binmode() function.

Thank you (both) for your responses.

Do I have to frame the output to STDOUT? I.e., do I need to create
a well-formed html document, etc? If not, how does the client file
know when the data stream has ended?
 
G

Gunnar Hjalmarsson

Richard said:
Thank you (both) for your responses.

Do I have to frame the output to STDOUT? I.e., do I need to create
a well-formed html document, etc?

The script shouldn't output any HTML at all. It's supposed to be
called by an said:
If not, how does the client file know when the data stream has
ended?

I don't understand what you mean by that.

Why don't you just try, and post your code here if you have a problem
with making it work.
 
R

Richard Trahan

Gunnar said:
Richard Trahan wrote:

Okay, thanks.

Part deux: I really want a way to run a script upon loading, without
having to use javascript (like onload()). I believe <img> is the only
way to do that. Is that right?
 
G

Gregory Toomey

Richard said:
Okay, thanks.

Part deux: I really want a way to run a script upon loading, without
having to use javascript (like onload()). I believe <img> is the only
way to do that. Is that right?

You are totally confused.

By wasy of example, have a look at the HTML I use to generate the stock
chart at www.float.com.au . I
The html contains
<img src="/scgi-bin/prod/gengraph.cgi?graph=XAO_graph_300x150" >

Here gengraph.cgi just writes a gif to stdout using the GD module (version
1.18), along these lines:

#!/usr/bin/perl
use strict;
use GD;

$|++;
$width= ....
$height= .....

print "Content-Type: image/gif\n\n";
my $im = new GD::Image($width,$height);
....
print $im->gif;


gtoomey
 
G

Gunnar Hjalmarsson

Richard said:
I really want a way to run a script upon loading, without having to
use javascript (like onload()). I believe <img> is the only way to
do that. Is that right?

Upon loading a HTML file? No. SSI (server side includes) is another
method that comes to mind. I believe that the <object> tag is yet
another way (even if I have never tried it).

But this is going off topic. Perl is the topic here. If you have
problems writing a Perl script that prints an image, please post what
you have so far, and somebody may help you fix it. Such a script can
be called via an <img> tag provided that it includes the printing of a
content-type header (which actually makes it a CGI script).
 
J

Joe Smith

Richard said:
I want to have a statement like this in an html file:
<img src="http://somewhere.com/cgi-bin/myscript.pl">

I want myscript.pl to deliver an image, which already exists
on the server.

1) Output the correct Content-Type: header.
2) Open the existing image file for reading, use binmode() on it.
3) Set STDOUT to binmode.
4) Read the input file a block at a time, print it.
5) Exit at EOF.

-Joe
 
R

Richard Trahan

Gregory said:
You are totally confused.

How could I be confused when your example does exactly what
I said, i.e., you use an <img> tag to run a script.

My point is that as an html file is loading into a browser,
there are only a few things that can appear in that file
that will cause the browser to (indirectly) run a script
located on some other machine. One is some javascript
function, the other is an image tag that contains -- as
does your example -- a src file which is really a Perl script.
(What are the others? I'm sure there are some, like php...)

Getting back to my original post, I want that script to
do something on my server and then -- only if it has to --
return a graphic. I'd rather it do nothing, but I don't know
if I can return a 505 header without confusing the browser,
which is expecting a graphic. But let's say I have to return a
graphic. One poster said I should just write binary to STDOUT. That
doesn't sound quite right. Another (two) say I should create
an image/gif header and then write binary. But when does the
browser know the data stream has ended? With regular html
files it sees </html>, or a length value in the header.

I could simply try all these things, but it's more fun trying
to emulate Middle Age monks who insisted they could divine
the number of teeth in a horse's mouth by theological speculation
rather than by dissecting the darn thing.
 
A

Alan J. Flavell

How could I be confused when your example does exactly what
I said, i.e., you use an <img> tag to run a script.

Er, no. You said you thought it was the *only* way.
My point is that as an html file is loading into a browser,
there are only a few things that can appear in that file
that will cause the browser to (indirectly) run a script
located on some other machine.

You won't be running your javascript on my browser anyway. Not until
you've given reason to believe that executing the js is more
beneficial to me than the risks which it involves.
One is some javascript function, the other is an image tag that
contains -- as does your example -- a src file which is really a
Perl script.

I'm sorry to have to say this, but "you are confused". At least, on
the evidence shown here.

I've always found that the best way to cope with complex problems
(aside from putting the PerlTroll into the killfile, since it only
creates confusion wherever it chooses to participate) is to divide the
solution into well-defined parts, and understand how each one works;
before assembling the parts into a coherent whole.
Getting back to my original post,

Well, getting back to your -original- post, you got an appropriate
answer to the question which you asked. But that unfortunately
(AFAICS) only demonstrated that you'd asked the wrong question, since
whatever it was that you *really* wanted to achieve had not been
revealed at the outset.
I want that script to do something on my server and then -- only if
it has to -- return a graphic.

I don't see anything there which needs to involve client-side
javascript. So either you aren't being candid with us, or you're
confused as to the need for some client-side js.
I'd rather it do nothing, but I don't know
if I can return a 505 header without confusing the browser,
which is expecting a graphic.

I can't help worrying that the more I read your posting, the more
confused I get. What is it - i.e in real world terms, not in terms of
whatever partially implemented draft solution that you had in mind -
are you trying to achieve?
But let's say I have to return a
graphic. One poster said I should just write binary to STDOUT.

Actually, if the result is already accessible on the server, it's
probably better to issue a status 302 redirect to its URL. But the
answer you got wasn't by any means wrong - it all hinges on what
you're trying to achieve, and that still seems to be a mystery...
That doesn't sound quite right. Another (two) say I should create
an image/gif header and then write binary.

Oh come on, be reasonable. If you're going to return a competent
response to a CGI script, then it's taken for granted that you need to
return a Content-type header first. I'm sure your informant assumed
that you would know that.
But when does the browser know the data stream has ended?

That's an interesting point, but not for the reason that you seem to
think. If the server actually knows the content length, then some
optimisations are feasible. And that's one of the points on which an
HTTP redirect could be helpful (keyword: cacheing). But it's not an
absolute requirement.
With regular html files it sees </html>,

Here you're at quite the wrong protocol layer of the network
or a length value in the header.

That part is OK, but it isn't mandatory.
I could simply try all these things, but it's more fun trying
to emulate Middle Age monks who insisted they could divine
the number of teeth in a horse's mouth by theological speculation
rather than by dissecting the darn thing.

Our equivalent would, I suppose, be to read the protocol
specifications ;-)

Keep in mind that you're returning a CGI response (which is properly
the topic of the CGI specification, see the CGI RFC draft which, for
whatever reason, seems to be getting nowhere at the moment, but is the
nearest we have to a specification). On the basis of which the HTTP
server will create an HTTP response, see RFC2616 for the applicable
details of /that/.

But these minutiae seem, at the moment, not to be what you need:
rather, you seem to need advice at a higher level, how to achieve
whatever it was (as yet unstated, AFAICS) that your whole process is
meant to achieve.

good luck
 
J

Jürgen Exner

Richard said:
How could I be confused when your example does exactly what

You are confused about what this NG is about. It is about the programming
language Perl.
I said, i.e., you use an <img> tag to run a script.

What does an said:
My point is that as an html file is loading into a browser,

What does a browser have to do with Perl?
there are only a few things that can appear in that file

"that file" is certainly not a Perl file.
that will cause the browser to (indirectly) run a script
located on some other machine.

The browser certainly doesn't run a Perl script. The browser connects to a
web server only.
One is some javascript
function,

JavaScipt is run client-side (normally at least) and has nothing to do with
Perl.
the other is an image tag that contains -- as
does your example -- a src file which is really a Perl script.
(What are the others? I'm sure there are some, like php...)

Pick your favourite from among the thousands of programming languages.
Any(!) will work. And that clearly indicates, that your question has nothing
to do with Perl at all.
Getting back to my original post, I want that script to
do something on my server and then -- only if it has to --
return a graphic. I'd rather it do nothing, but I don't know
if I can return a 505 header without confusing the browser,
which is expecting a graphic. But let's say I have to return a
graphic. One poster said I should just write binary to STDOUT. That
doesn't sound quite right. Another (two) say I should create
an image/gif header and then write binary. But when does the
browser know the data stream has ended? With regular html
files it sees </html>, or a length value in the header.

So you don't even know _what_ your Perl program is supposed to print!
Find out what you CGI program is supposed to return first.

Then I am sure people in this _Perl_ newsgroup will be happy to help you
with generate that desired output.
I could simply try all these things, but it's more fun trying
to emulate Middle Age monks who insisted they could divine
the number of teeth in a horse's mouth by theological speculation
rather than by dissecting the darn thing.

Yeah, but you are trying to count the number of grains on a oat stem to find
the number of teeths.

In short: you are barking at the wrong tree. Ask in a NG that actually deals
with HTTP/HTML/CGI/<IMG> and those things.

jue
 
B

Bob Walton

Richard said:
I want to have a statement like this in an html file:
<img src="http://somewhere.com/cgi-bin/myscript.pl">

I want myscript.pl to deliver an image, which already exists
on the server.

How do I do that?


Consider something like the following example (but read in your existing
image rather than generating one as this example does):


use CGI qw:)standard);
use GD;
if(param('image')){
print "Content-type: image/png\n\n";
binmode STDOUT;
$im = new GD::Image(100,100);
$lightpurple=$im->colorAllocate(255,200,255);
$black=$im->colorAllocate(0,0,0);
$im->interlaced('true');
$im->line(param('n1'),param('n2'),param('n3'),
param('n4'),$black);
print $im->png;
exit;
}
print header,
start_html('GD Example'),
h1('GD Example'),
start_form,
textfield('n1'),p,
textfield('n2'),p,
textfield('n3'),p,
textfield('n4'),p,
"Enter four numbers from 0 to 100 and submit",p,
submit,
end_form,
hr;
if(param()){
print img{src=>'GDtest.pl?n1='.param('n1').
'&n2='.param('n2').'&n3='.param('n3').
'&n4='.param('n4').'&image=yes',width=>100,height=>100};
}
print end_html;
 
J

Joe Smith

Richard said:
graphic. One poster said I should just write binary to STDOUT. That
doesn't sound quite right. Another (two) say I should create
an image/gif header and then write binary.

That's exactly what you should be doing.
But when does the browser know the data stream has ended?

The end of the HTTP data stream is indicted by the end-of-file condition.
In your case, this is when the CGI script closes STDOUT when it exits.
-Joe
 
R

Richard Trahan

Alan said:
On Sun, 18 Jul 2004, Richard Trahan wrote:

I'm sorry, I do not engage in ad hominem attacks,
nor do I respond to such attacks made by insecure
people who find respite in degrading others in the
hope of self aggrandizement. Therefore I will not
respond to your further posts, nor will I read
your response to this one, so don't waste your time.
 
R

Richard Trahan

Jürgen Exner wrote:

The last field of this ng's title is "misc". I consider
any application written in Perl to fall into that
category.

Since you appear to be a self-appointed ng policeman,
a role which many unhappy would-be programmers assume,
I will ignore your posts, including any response to
this one.
 
R

Richard Trahan

Joe Smith wrote:

Thank you for your precise and courteous reply. It
has answered my questions, unlike the responses of
certain malcontented authors of previous posts.

The net is, after all, a haven for unhappy souls.
 
A

Anno Siegel

Richard Trahan said:
I'm sorry, I do not engage in ad hominem attacks,

Where on earth do you see even a hint of an ad hominem attack in
Alan's post?
nor do I respond to such attacks made by insecure
people who find respite in degrading others in the
hope of self aggrandizement.

"Attacks"? "Self aggrandizement"? You must have been reading a
different posting.
Therefore I will not
respond to your further posts, nor will I read
your response to this one, so don't waste your time.

Your loss.

Anno
 
S

Sherm Pendley

Richard said:
The last field of this ng's title is "misc". I consider
any application written in Perl to fall into that
category.

If you're using DreamWeaver to edit HTML, would that make HTML a
legitimate topic for discussion in a C++ group? After all, DW is
probably written in C++, and any output of such an application is fair
game there, right?

To answer your question - "how does a browser know when the end of an
image is reached": The same way it would if the image were produced by
any other language, be in Python, C, Java, or whatever. See? It's not a
question about Perl, it's a language-agnostic question about the CGI
environment.

Try asking in comp.lang.infosystems.www.authoring.cgi.
Since you appear to be a self-appointed ng policeman,
a role which many unhappy would-be programmers assume,
I will ignore your posts, including any response to
this one.

Ad hominem attacks do nothing to help your case.

sherm--
 
I

Ilmari Karonen

The last field of this ng's title is "misc". I consider
any application written in Perl to fall into that
category.

The name of this newsgroup has four parts. *All* of them are
significant. Your question is indeed about _comp_uting, and it
certainly is _misc_ellaneous, but it has nothing to do with the
programming _lang_uage called _Perl_.

So this thread is off-topic. Really.
 

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

Staff online

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top