http://www.perlmonks.org?node_id=189153

r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone - wow, it's been a long time since I posted here (school, work, etc. taking all my time) - I missed this place!

Anyway, in my quest to finally get back to Perl after a year of using Java and C in a forced academic enviornment, I decided to start playing around with the GD.pm module from Lincoln Stien.

I have an image gallery on my website, and I wanted to (because I use Image::Magick to do dynamic resizing anyway) add dynamic text to the images such as a caption that will be held in a database somewhere else. Therefore, GD immediatley came to mind.

So, say I have a file my_image.png, and I want to use GD to simply add the string "TEST!" to the top left-hand corner of the image and re-print it:
use GD; # read the image my $gdimg = GD::Image->newFromPng("my_image.png"); # we need a color for our text my $green = $gdimg->colorAllocate(0,255,0); # write the string "TEST!" to the image $gdimg->string(gdMediumBoldFont, 5, 5, "TEST!", $green); # get the new image my $data = $gdimg->png; # write it out binmode STDOUT; print $data;
Running the above script, color pallete becomes GREATLY reduced, even though the image format stayed the same (PNG all the way through). The same happened if the calls to $gdimg->colorAllocate() and $gdimg->string() are completely removed, so that basically all that is happening is a read/write of the file through the GD interface. Printing out $gdimg->colorsTotal() returns only 256. As an example, check out these two images:

Original: New Window
GD'ed: New Window

As you can see from even these small thumbnail files, the color pallete has been reduced unacceptably (and it's even worse for larger files). Does anyone have any ideas why this might be happening? Thanks a bunch everyone - it's great to be back!

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov