#!/usr/bin/perl -w

#counter.cgi
#	outputs an image for the given number
#

#setup environment
use GD;
use GD::Text;
use CGI::Pretty;

#set to 1 to save images
my $saveImages=0;

#init cgi object to get environment variables
my $q=new CGI::Pretty;

#get variables from environment or sert default values
my $fgcolor=$q->param('fgcolor') || 'green';
my $bgcolor=$q->param('bgcolor') || 'black';
my $num=$q->param('num') ||'0001';
my $width=$q->param('width');
my $height=$q->param('height');
my $font=$q->param('font') || 'medium';
my $fsize=$q->param('fsize') || '';
my $padding=$q->param('padding') || 5;
my $border=$q->param('border') || 0;
my $bordercolor=$q->param('bordercolor') || 'white';

# allocate some colors
my %colors=();
$colors{'white'}=[255,255,255];
$colors{'black'}=[0,0,0];
$colors{'red'}=[255,0,0];
$colors{'blue'}=[0,0,255];
$colors{'green'}=[0,255,0];
$colors{'gray'}=[169,169,169];
$colors{'darkgray'}=[128,128,128];
$colors{'lightgray'}=[211,211,211];
$colors{'pink'}=[255,192,203];
$colors{'darkred'}=[139,0,0];
$colors{'lightblue'}=[173,216,230];
$colors{'darkblue'}=[0,0,139];
$colors{'cyan'}=[0,255,255];
$colors{'magenta'}=[255,0,255];
$colors{'yellow'}=[255,255,0];
$colors{'lightyellow'}=[255,255,224];
$colors{'lightcyan'}=[224,255,255];
$colors{'darkcyan'}=[0,139,139];
$colors{'darkmagenta'}=[139,0,139];
$colors{'purple'}=[255,0,255];

#setup font info
$font=gdGiantFont if (lc($font) eq 'giant');
$font=gdLargeFont if (lc($font) eq 'large');
$font=gdMediumBoldFont if (lc($font) eq 'medium');
$font=gdSmallFont if (lc($font) eq 'small');
$font=gdTinyFont if (lc($font) eq 'tiny');

#create new Text object, so we can get the size info to build image
my $text=new GD::Text() || (print GD::Text::Align::error() && die);
$text->set_font($font,$fsize);
$text->set_text("$num");
$width=$text->get('width')+(2*$padding)+(2*$border) unless $width;
$height=$text->get('height')+(2*$padding)+(2*$border) unless $height;

#get center of image for aligning text
my $textx=($width/2)-($text->get('width')/2);
my $texty=($height/2)-($text->get('height')/2);

#create new Image
my $pic=new GD::Image($width,$height);

#set foreground and background colors
$bgcolor='black' unless
my @bgclr=[0,0,0];
web2rgb($bgcolor,\@bgclr) || (@bgclr=(@{$colors{$bgcolor}}));# || @{$colors{'black'}}));
my @fgclr=[0,0,0];
web2rgb($fgcolor,\@fgclr) || (@fgclr=(@{$colors{$fgcolor}}));# || @{$colors{'green'}}));

#draw background
$pic->fill($width,$height,$pic->colorAllocate( @bgclr ));

#if border is defined and >o, draw border
if ($border){
	my @borderclr;
	web2rgb($bordercolor,\@borderclr) || (@borderclr=@{$colors{$bordercolor}});# || @{$colors{'darkblue'}});
	$pic->setThickness($border);
	$pic->rectangle($border/2,$border/2,
					$width-($border/2),$height-($border/2),
					$pic->colorAllocate( @borderclr ));
}

#draw the string
$pic->string($text->get('font'),$textx,$texty,$num,$pic->colorAllocate( @fgclr ));

print $q->header('image/jpeg');
binmode STDOUT;
print $pic->jpeg();

if ($saveImages){
	my $file="perl/counterimg/$num\_$bordercolor.$border\_$bgcolor\_$fgcolor\_$font.jpg";
	if (open(IMG,">$file")){
		print IMG $pic->jpeg();
		close IMG;
	}
}


sub web2rgb{
	my ($web,$colors)=@_;
	if ($web=~/^0x([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/){
		$$colors[0]=hex $1;
		$$colors[1]=hex $2;
		$$colors[2]=hex $3;
		#print "\n**************\ncolors:$$colors[0],$$colors[1],$$colors[2]\n*****************\n";
		return 1;
	}
	return 0;
}