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

Is it possible to use raw image data in a CGI script?

I have a function that returns the contents of an image file. I don't have the actual file, it's in a DB somewhere. I just get the binary data, as if I'd read it in as text. I'd like to wrap a CGI around this so that users can look up an image and see it on the page. If I can do it without writing that image data to a temp file, that would be really cool. Is that possible?

If not, is there some good way of handling the temp files? Seems like there's all sorts of issues there, from when to delete them to how to choose non-conflicting names.

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: CGI: Raw image data
by friedo (Prior) on Apr 28, 2005 at 23:21 UTC
    You can send the raw image data directly to the browser as long as you know the image type.

    use CGI; my $q = CGI->new; my $imgdata = get_image_data(); print $q->header( 'image/jpeg' ); # or image/gif, etc. print $imgdata;
      Don't forget to add
      binmode STDOUT;
      before you print the image data, just to be safe.
      Can I do that in the middle of a page, though? I actually need to display several of these images on a page, along with some UI and text.

      ---
      A fair fight is a sign of poor planning.

        For each image you can make the images URL refer to a CGI like the one he gave you so that each image is a call to the CGI.


        ___________
        Eric Hodges

        You could use the data: url scheme, but you'll find browser support a little lacking (Internet Explorer is among those browsers without support).

        You'll need to, as previously mentioned, have the user agent make additional requests for each image.