throop has asked for the wisdom of the Perl Monks concerning the following question:

Can a cgi-bin program return a graphics file for an IMG tag?

I have a program which generates graphs, built on to of GD::Graph. Right now, it writes a .gif file. I'd like to modify the code and call it as a cgi-bin file so that it generates the graph as needed, without ever writing to a file. That is, I'd like to have an html page with the text

Here's a graph: <IMG src=http://myHost/cgi-bin/imageGen.cgi?data=2,3&data=5,4&xlabel +=Jan,Feb>
and have the graph appear without ever saving an image file. Can someone point me in the right direction? Thanks.

Replies are listed 'Best First'.
Re: GD::Graph called from cgi-bin
by kcott (Archbishop) on Oct 30, 2010 at 01:21 UTC

    First, this just theory - I haven't tried it! (I have done similar things but not this specific scenario.)

    HTML will expect the value of the src attribute to be a resource which returns an image.

    If your imageGen.cgi does something like:

    my $gd = $graph->plot(\@data); print STDOUT $gd->gif;

    I believe that should serve up the image as you want. (I note from the GD::Graph doco that you may need to use binmode - I didn't read it in any great detail.)

    If this image represents some rapidly changing data, then creating images on the fly may be the way to go. However, I would consider the processing overhead of repeatedly generating the same image versus the storage overhead involved in creating it once and saving it.

    -- Ken

Re: GD::Graph called from cgi-bin
by locked_user sundialsvc4 (Abbot) on Oct 30, 2010 at 01:48 UTC

    When the browser encounters an IMG-tag, it will issue a request for the image data.   By far the easiest way to see how this works is to use a client-side debugger such as Firebug.

    If you want your Perl program to serve the image-data, the key factor that you must contend with is properly setting the content-type and other relevant HTML tags in the HTTP response-packet that you send back.   Of course, every Perl HTTP-package provides the means to do this.

    As it happens, I like to “cheat” this.   If the image that needs to be sent back is literally just “the content of such-and-such static disk file,” then I like to arrange the Apache configuration so that Apache can directly do what Apache already does best:   serving static files.   I only send to the Perl script things that actually demand a programmatic response.

    I suggest (from experience...) that the best approach to take here is two-step.   First, observe and study what needs to be done, by watching existing HTTP interactions between clients and servers using the debugging tools I have described.   Then, look at how to generate these responses with Perl, and at how to prepare Apache (or IIS) configurations so as to route the various kinds of requests in appropriate directions.   It might feel a bit bewildering at first, heh..., but it’s really not complicated at all.

Re: GD::Graph called from cgi-bin
by dasgar (Priest) on Oct 30, 2010 at 05:20 UTC

    Yep, you're definitely headed in the right direction. I'm doing something like this at work where I'm generating images on the fly using GD::Image. Basically, I have one script that is the main page. It reads in data from a file and for the graphics, it calls a second script in the HTML img tag and passes data to it just like your img tag has. That second script uses the CGI module to read in the values passed to it, creates an image, applies binmode, and then prints the image. Basically, this is doing what kcott described.

    In my case, I'm dealing with less than 10 images that are "gauges" of the latest readings, which are collected every 15 minutes. Also, the page isn't viewed very often.

    Try it out. If you encounter any problems, just come back and ask for help.

Re: GD::Graph called from cgi-bin
by Khen1950fx (Canon) on Oct 30, 2010 at 01:11 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.