in reply to How can I use a CGI script to return an image?

Supposing your image is in a file, and you want your script to stream its contents on-demand:
open IMAGE, "/path/to/image.jpg"; #assume is a jpeg... my ($image, $buff); while(read IMAGE, $buff, 1024) { $image .= $buff; } close IMAGE; print "Content-type: image/jpeg\n\n"; print $image;
or, faster and easier:
use File::Copy; print "Content-type: image/jpeg\n\n"; copy "/path/to/image.jpeg", \*STDOUT;

Replies are listed 'Best First'.
RE: Answer: How can I use a CGI script to return an image?
by merlyn (Sage) on Sep 07, 2000 at 16:51 UTC
    Or simpler (because I hate typing):
    use File::Copy; print "Content-type: image/jpeg\n\n"; copy "/path/to/image.jpeg", \*STDOUT;
    And then you don't even have to type binmode STDOUT as another monk in this thread suggested, because copy does it for you.

    -- Randal L. Schwartz, Perl hacker

RE: Answer: How can I use a CGI script to return an image?
by Jouke (Curate) on Sep 07, 2000 at 15:29 UTC
    don't forget
    binmode STDOUT;
    before you print the image to standard out... Jouke Visser, Perl 'Adept'