On Mon, Feb 2, 2009 at 8:26 AM, bacoms <googlebr...@balfour.me.uk> wrote:
> Is it possible to eliminate the need to first write the image to
> disk?

Yes, it is possible and desirable.  Writing the images to disk creates
a lot of complications.  You have to generate some random temporary
name to avoid having train wrecks.  If two users hit the page at about
the same time, the output from the first could be overwritten by the
second.  You also have to worry about cleaning up the images so they
do not accumulate, consuming disk space.  Last, but not least, there
are security issues involved with scripts writing to the disk on the
server.

>
> All my attempts at trying this have all failed. The best I get is with
> the following code:
>
>     print "Content-type: text/html\n\n";

>                          etc
>                          etc
>      my($myImage) = $myChart->plot(\...@data) or die $myChart->error;
>      print $myImage->png;
>
> which produces gibberish where the chart should be. (cribbed from
> http://linuxgazette.net/issue83/padala.html )
>
> I suspect is that this specifies "Content-type: image/png\n\n" whereas
> my web page specifies "Content-type: text/html\n\n".
>
You need to supply the proper header for the image or the browser will
attempt to display your image data as text.  This will produce
gibberish on the screen.

Remember that the header has to be the first thing output to the
browser, otherwise it will be treated as part of the <body> of the
page, printed as text and not used to interpret the data as an image.

print "Content-type: image/png\n\n";

Also, you need to specify binmode for STDOUT and you want unbuffered
output for stdout by setting the magic perl var $| to a non-zero
value.

Without unbuffered output some of  your data may be cached instead of
sent directly to the client.  This could be seen as a corrupted image
by the browser.  Without binmode some of the image data will either
get munged or interpreted as some non-ascii characters (non-english
special chars, graphics chars, etc.)

$|++;
binmode STDOUT;

> Can it be done and how????

After making the changes suggested above, make an <img src=....path
and name of your script that generates the image...

So, you put the code that generates the image in a separate script,
then call that script where you want the image to appear with an <img>
tag.  You can also have a page that just outputs the image, but then
you cannot have any other content, just the image.

A couple debugging tactics:

Make sure your code is actually producing the image.

You can either do this by commenting out the content-type head and
redirecting the output.
./test.cgi >test.png

Then look at the image to make sure the image is okay.

You can also run the image at the command line without removing the
header. then if you look at the output with a text editor you should
see the content-type header at the top, followed by a blank line then
the raw image data.  Delete the first two lines, save the file and
display the image.  This will verify that you got the header right,
and nothing before it, and that the image is correct.  Depending on
your OS and editor this may or may not work for you.  On my linux box
with vi it works fine.

If you have LWP on your system, try using HEAD and wget to fetch your
image script to verify that you get the right stuff.

HEAD http://blahblahbl/cgi-bin/imagetest.cgi

Should show the correct image/png header.

And:
wget http://blahblahbl/cgi-bin/imagetest.cgi

should fetch a local copy image that you can view on your system.

Let us know how this works out.  I'm not entirely awake and haven't
done this in quite a while, so I may have missed something.  This
should at least get you on the right track.

Cheers,

Mike

-- 
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/


Reply via email to