# Example 2-3. The BrokenImage module package BrokenImage; use strict; use GD; # use GD to draw the image # Define some different styles of broken images sub black_box { # A black box with text in yellow # Takes a string of text as a parameter. my $self = shift; my $text = shift; my $image = new GD::Image(6 * length($text) + 10, 20); my $black = $image->colorAllocate(0, 0, 0); my $yellow = $image->colorAllocate(255, 255, 0); $image->string(gdSmallFont, 5, 3, $text, $yellow); print_image($image); } sub icon { # An icon with an error code. # Takes a short string as a parameter. my $self = shift; my $code = shift; my $image = new GD::Image(43,48); my $white = $image->colorAllocate(255, 255, 255); my $black = $image->colorAllocate(0, 0, 0); my $gray = $image->colorAllocate(213, 213, 213); $image->filledRectangle(4, 4, 42, 47, $black); $image->filledRectangle(0, 0, 38, 43, $gray); $image->rectangle(0, 0, 38, 43, $black); $image->string(gdMediumBoldFont, 3, 10, "error", $black); $image->string(gdMediumBoldFont, int(20-3.5*length($code)), 21, $code, $black); print_image($image); } sub print_image { # Print the image my $image = shift; use CGI; my $query = new CGI; print $query->header(-type => "image/png", -expires => '0s'); print $image->png; } 1; # Modules should return true