Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by Anonymous Monk
on Jun 17, 2000 at 01:51 UTC ( [id://18565]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

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?

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: How can I use a CGI script to return an image?
by lhoward (Vicar) on Jun 17, 2000 at 02:07 UTC
    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;
Re: How can I use a CGI script to return an image?
by cianoz (Friar) on Sep 07, 2000 at 15:19 UTC
    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;
      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

      don't forget
      binmode STDOUT;
      before you print the image to standard out... Jouke Visser, Perl 'Adept'
Re: How can I use a CGI script to return an image?
by knowmad (Monk) on Oct 13, 2005 at 22:43 UTC

    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
      Sure... but why are you pulling down a gif and sending out a jpeg? The Content-type of the file you fetch and the file you send on should match.

      Gary Blackburn
      Trained Killer

Re: How can I use a CGI script to return an image?
by xjar (Pilgrim) on Sep 07, 2000 at 01:03 UTC
    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";
Re: How can I use a CGI script to return an image?
by damian1301 (Curate) on Nov 10, 2000 at 08:27 UTC
    You people make everything so damn hard. I think hes basically looking for...

    #!/usr/bin/perl use CGI img; print img{src =>"blah.gif", #or whatever the path is... height=>"400", #or whatever the height is.. width=>"200", #or whatever the width is... border=>"0", #or whatever you want your border to be... };
    And if you want that neat litte page.pl?image2 thing going then go with what xjar said, that seems to be the best.
      I disagree. Re-read his question. He's wanting to put a CGI script as the <img src="...">. Thus, his CGI script needs to output a valid binary image instead of the usual text/html.
Re: How can I use a CGI script to return an image?
by bajada (Initiate) on Apr 10, 2004 at 11:18 UTC
    I got the same problem and I tried this:
    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;
    It worked perfectly on a 2K gif image, but however with larger images of 5K or so, only about half of the image shows up. I tried to change the buffer value but all in vain. Any help is greatly appreciated.

    Originally posted as a Categorized Answer.

Re: How can I use a CGI script to return an image?
by Anonymous Monk on Sep 03, 2004 at 03:48 UTC
    damian1301, I do not think that he was looking for that. Your example does nothing but output an HTML image tag. The other examples produced the image itself on the server and sent it to the browser.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://18565]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-06-16 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.