#!/usr/bin/perl -wT use strict; use Image::Magick; use CGI; my $conf = { font_size => 8, font_name=>'/srv/www/htdocs-devel/lib/kroe0555.ttf', img_type=>'png', }; my $t = new CGI(); my $string = $t->param('string'); $string ||='leocharre'; $$conf{string} = untaint( $string ); # attempt untaint $$conf{img_height} = int($$conf{font_size}*1.33) ; $$conf{img_width} = ( length($$conf{string}) * int($$conf{font_size}*0.8) ); my $bgcolor = $t->param('bgcolor'); my $fgcolor = $t->param('fgcolor'); $bgcolor ||='#fff'; $fgcolor ||='#000'; $$conf{bgcolor} = untaint_color($bgcolor); $$conf{fgcolor} = untaint_color($fgcolor); generate_img($conf); show_and_exit($conf); sub show_and_exit { my $conf = shift; # read in image and output to browser print(qq|Content-Type: image/$$conf{img_type}\n\n|); binmode STDOUT; $$conf{img}->Write($$conf{img_type}.':-'); exit; } sub generate_img{ my $conf = shift; $$conf{img} = Image::Magick->new; $$conf{img}->Set(size => $$conf{img_width}."x".$$conf{img_height}); $$conf{img}->ReadImage('xc:'.$$conf{bgcolor}); $$conf{img}->Annotate( font=> $$conf{font_name}, pointsize=> $$conf{font_size}, fill=> $$conf{fgcolor}, text=> $$conf{string}, gravity=> 'center' ); } sub untaint { my $string= shift; if ($string=~/^([a-zA-Z0-9 \.\:\[\]\{\}\+\-_]+)$/){ return $1; } die ("don't like some chars here = [$string]"); } sub untaint_color { my $color = shift; if ($color=~/^([\d\w#]{3,7})$/){ return $1; } die ("the heck kind of color is this? [$color]"); }