Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
Do you know where your variables are?

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

( #18565=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 17, 2000 at 01:51 UTC
Q&A  > CGI programming


I want to be able to display an image from a image tag e.g. <img src="">

Is it possible to do that?

Answer: How can I use a CGI script to return an image?
contributed by lhoward

I assume you mean "how do I have a CGI program called from an image and have the CGI program pipe an image back to the IMG tag for display"?

First off is the image tag. Nothing special here, it should looks just like a normal image tag except it calls your CGI and passes the apropriate parameters to it:

<img src="img_gen.cgi?size=100">
The interesting part is in your CGI. You need to return apropriate HTTP headers for the image (Content-Type: image/gif or whatever is apropriate). and then just pipe the image contents back. Here's a small CGI program that returns an image, its size based on the passed parameter
#!/usr/bin/perl -w use strict; use CGI; use GD; my $cgi=new CGI; my $cgi_size=$cgi->param('size') || '50'; print "Content-type: image/gif\n\n"; my $gd=new GD::Image($cgi_size,$cgi_size); my $blue=$gd->colorAllocate(0,0,255); $gd->fill(0,0,$blue); binmode STDOUT; #just in case we're on NT print $gd->gif;
contributed by cianoz

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;
contributed by knowmad

In case the OP was actually wanting to use the script to return an image, not html, here's another way to do it. This script uses LWP to grab an image from another server and send it along. You can use it inside the src attribute of an image tag.

use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $response = $ua->get(''); die "Unable to retrieve image." unless ($response->is_success); print "Content-type: image/jpeg\n\n"; print $response->content;
contributed by xjar

If lhoward's answer isn't what you were looking for exactly (i am only assuming this because you said you were a perl newbie and you probably wouldn't have any GD knowledge if you were :) then this might be what you're looking for. Rather than using an

<img src="...">

tag, you could use an href to go to the perl script which will then display the specified image file like this

<a href="/cgi-bin/image.cgi?image=image.jpg">

In this way, you can have one script display all your images based on the filename in the URL, and each image will be displayed on a page with the same "Look and Feel" without having to write multiple pages.

#!/usr/bin/perl use strict; use CGI; my $cgi = new CGI; my $image = $cgi->param('image'); print "Content-type: text/html\n\n"; print "<html><head><title>$image</title></head>\n"; print "<body>\n"; print "<center>\n"; print "<img src=/images/$image><p>\n"; #The path here would obviously +be different according to where your images are at print "</center>\n"; print "</body>\n"; print "</html>\n";


  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (3)
    As of 2014-03-17 03:47 GMT
    Find Nodes?
      Voting Booth?

      Have you used a cryptocurrency?

      Results (340 votes), past polls