in reply to Re^2: Perl GD problem with colors for text
in thread Perl GD problem with colors for text

The GD documentation for the colorAllocate function says:
If no colors are allocated, then this function returns -1.

Checking $mycolor, I found that it was set to -1 for all 2553 possible RGB combinations.

Assumption: Because the string function received -1 for the color value, it used the (first|last|?) value in the image's existing palette, thus explaining why the text appeared in the same color as the box.

I then converted test.jpg to test.gif with an image editor, and changed $jpg to "test.gif". $mycolor received a positive value, and the text appeared in the chosen color.

I then tried an explicit GD::Image->newFromJpeg on test.jpg, to no avail. I also tried converting to test.png, also to no avail.

It appears that colorAllocate works correctly when starting with .gif images, but not for .jpg or .png.

Replies are listed 'Best First'.
Re^4: Perl GD problem with colors for text
by natol44 (Sexton) on Nov 28, 2009 at 08:23 UTC
    Thank you for your time!

    I converted into GIF and yes, it works, on *this* picture. Using other pictures, if I try to print text in another color in the same line (2 colors on the same line), again it bugs.

    $red = $im->colorAllocate(255,0,0); $green = $im->colorAllocate(0,255,0);
    $im->string(gdSmallFont,555,6,$testline1,$red);
    $im->string(gdSmallFont,628,6,$testline2,$green);

    A new pic has been uploaded at http://www.2shared.com/file/9508604/28e97b03/lr1_fee.html

    The Green shows in "white"... Moreover working in GIF makes strange things in the pics that show up with defects in my application. I wonder if there isn't a way to make it work in PNG? After all GD is supposed to support PNG too...

    Thank you
Re^4: Perl GD problem with colors for text
by gmargo (Hermit) on Nov 28, 2009 at 08:00 UTC

    I had success with the newFromJpeg constructor, with the $truecolor flag set to 1.

    my $im = GD::Image->newFromJpeg( $jpg, 1 );

    It was quite educational to run the identify -verbose command (from the ImageMagick suite) on the "before" and "after" images.