Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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


Description:

I want to be able to display an image from a image tag e.g. <img src="http://host.com/cgi-bin/image.cgi?image2">

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;
Answer: How can I use a CGI script to return an image?
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;
Answer: How can I use a CGI script to return an image?
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('http://www.perl.org/simages/lcamel.gif'); die "Unable to retrieve image." unless ($response->is_success); print "Content-type: image/jpeg\n\n"; print $response->content;
William
Answer: How can I use a CGI script to return an image?
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 (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2014-12-19 07:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (73 votes), past polls