| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
use CGI; |
| 4 |
use Imager; |
| 5 |
|
| 6 |
my $cgi = CGI->new; |
| 7 |
|
| 8 |
my $color = $cgi->param('color'); |
| 9 |
|
| 10 |
defined $color or $color = ''; |
| 11 |
|
| 12 |
# Imager allows a number of different color specs, but keep this |
| 13 |
# simple, only accept simple RRGGBB hex colors |
| 14 |
my %errors; |
| 15 |
|
| 16 |
# note that you need to perform validation here as well as in |
| 17 |
# the form script, since the user can view image and manipulate the |
| 18 |
# URL (or even fetch the URL using LWP and modify the request in any way |
| 19 |
# they like. |
| 20 |
# Since we're producing an image, we don't have a mechanism to |
| 21 |
# report errors (unless we choose to draw text on an image), so |
| 22 |
# just product a default image. |
| 23 |
if (!defined $color || $color !~ /^[0-9a-f]{6}$/i) { |
| 24 |
$color = '000000'; |
| 25 |
} |
| 26 |
|
| 27 |
my $im = Imager->new(xsize=>40, ysize=>40); |
| 28 |
$im->box(filled=>1, color=>$color); |
| 29 |
|
| 30 |
# this will force the flushing of the headers, otherwise the server (and |
| 31 |
# your web browser) may see the image data before the content type. |
| 32 |
++$|; |
| 33 |
|
| 34 |
print "Content-Type: image/jpeg\n\n"; |
| 35 |
|
| 36 |
# use binmode to prevent LF expanding to CRLF on windows |
| 37 |
binmode STDOUT; |
| 38 |
|
| 39 |
# we have to supply the type of output since we haven't supplied a |
| 40 |
# filename |
| 41 |
$im->write(fd=>fileno(STDOUT), type=>'jpeg') |
| 42 |
or die "Cannot write to stdout: ", $im->errstr; |
| 43 |
|
| 44 |
=head1 NAME |
| 45 |
|
| 46 |
samp-image.cgi - demonstrates interaction of HTML generation with image generation |
| 47 |
|
| 48 |
=head1 SYNOPSIS |
| 49 |
|
| 50 |
/cgi-bin/samp-image.cgi?color=RRGGBB |
| 51 |
|
| 52 |
=head1 DESCRIPTION |
| 53 |
|
| 54 |
This is the image generation program that samp-form.cgi uses to |
| 55 |
generate the image. |
| 56 |
|
| 57 |
See samp-form.cgi for more detail. |
| 58 |
|
| 59 |
=head1 REVISION |
| 60 |
|
| 61 |
$Revision$ |
| 62 |
|
| 63 |
=head1 AUTHOR |
| 64 |
|
| 65 |
Tony Cook <[email protected]> |
| 66 |
|
| 67 |
=cut |