in reply to using GD inside CGI...

Unless there's a good reason to generate the HTML and the image at the same time, I would use a seperate CGI to just generate the image data:
# in the HTML generating code: # set up headers and other html... print "<img src='/cgi-bin/my-image.cgi?some=params'>";
and...
# in the /cgi-bin/my-image.cgi script: # set up the GD::Image in $image print $cgi->header("image/png"); # set the correct content-type binmode STDOUT; print $image->png; # end