tags:

views:

428

answers:

3

My google fu in failing me. How do I use Perl to serve up an already generated image?

Example:

<http><body><img src="getimage.pl"></body></html>

What goes in getimage.pl?

+2  A: 

WWW FAQs: "How do I output images from a Perl/CGI or PHP script" should get you going in the right direction. You will have to forgive me for not answering your question directly as I haven't touched Perl in about 5 years.

Jordan S. Jones
+3  A: 

Here you go:

#!/usr/bin/perl -w
my $file = "inner-nav.gif";
my $length = (stat($file)) [10];
print "Content-type: image/gif\n";
print "Content-length: $length \n\n";
binmode STDOUT;
open (FH,'<', $file) || die "Could not open $file: $!";
my $buffer = "";
while (read(FH, $buffer, 10240)) {
    print $buffer;
}
close(FH);
RichieHindle
Very simple and no need to load CGI, thanks.
Nifle
RichieHindle, please use Perl::Critic - we should give advices with modern Perl
Alexandr Ciornii
Sorry - my Perl is extremely rusty. That was a (tested) example I found on the net.
RichieHindle
Instead of pushing people toward Perl::Critic, just say "use three argument open". :)
brian d foy
Is there something missing? - as my browser indicates it is still in the busy loading state (denoted by animated loading symbol) even after the image is displayed in the browser. Should there be something added to end of the response sent back to the client, to indicate loading complete?
Rob
+4  A: 

Something like this ...

#!/usr/bin/perl

use strict;
use warnings;
use CGI;

my $gfx='';
$gfx = makeImage();
print CGI::header( type=>'image/png',
                   expires=>'+1m',
                   content_length=>length($gfx)});
print $gfx;
Thanks, I wish I could mar your answer as an accepted answer too.
Nifle