⭐ in reply to How can I use a CGI script to return an image?
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:
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<img src="img_gen.cgi?size=100">
#!/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;
|
|---|