Listing 1. gd-intro.pl

#!/usr/bin/perl -wT
# gd-intro.pl
use strict;
use diagnostics;
use CGI;
use GD;
# Create an instance of CGI
my $query = new CGI;
# Create a new image object
my $image = new GD::Image(100,100);
# Allocate some colors
my %COLORS;
$COLORS{white} = $image->
   colorAllocate(255,255,255);
$COLORS{blue} = $image->
   colorAllocate(0,0,255);
$COLORS{green} = $image->
   colorAllocate(0,255,0);
# Make the object interlaced for nicer loading
$image->interlaced(1);
# Set the background color to white
$image->transparent($COLORS{white});
# Draw a green rectangle
$image->rectangle(20, 20, 80, 80,
   $COLORS{green});
# Fill it with blue
$image->fill(50, 50, $COLORS{blue});
# Send an appropriate MIME header
print $query->header(-type => "image/gif");
# Send a GIF version of the object
print $image->gif;