I'm trying to write text onto an image and when I use ImageTTFText, it works, but only in light blue or black. In the following example, the first two imageTTFtexts show up light blue and the third one show up black. What's strange (to me) is that the colors for the first two were allocated, but the color for the third one was not. Here's my code:

<?php 

Header("Content-Type: image/png");

$im=imageCreateFromJpeg("bgregsmiling.jpg");

$black = ImageColorAllocate($im, 0, 0, 0);

$white = ImageColorAllocate($im, 255, 255, 255);

$font="/home/virtual/site2/fst/var/www/html/fonts/ALPHA.TTF"; 

imageTTFtext($im,50,0,20,100,$black,$font,"Greg");

imageTTFtext($im,50,0,20,200,$white,$font,"Greg");

imageTTFtext($im,50,0,20,300,$red,$font,"Greg");

imagepng($im); 

imageDestroy($im); 

?> 

    I think you have to allocate 2 other colors:

    $blue = ImageColorAllocate($im, 0, 0, 255);
    $red = ImageColorAllocate($im, 255, 0, 0);

      Hmm, I tried that and then they're all light blue. It seems like if the color is allocated, it's automatically light blue.

        try creating a temporary image, allocate the colors from the temp image, then copy the source image to the temporary image and put on the text.

          Hey hey hey, that did it!

          Thanks so much!

          Here's my working code for people who have trouble in the future:

          <?php
          Header("Content-Type: image/jpeg");
          $size=GetImageSize("bgregsmiling.jpg");
          $w=($size[0]);
          $h=($size[1]);
          $im1 = ImageCreate($w, $h);
          $black = ImageColorAllocate($im1, 0, 0, 0);
          $white = ImageColorAllocate($im1, 255, 255, 255);
          $blue = ImageColorAllocate($im1, 0, 0, 255);
          $red = ImageColorAllocate($im1, 255, 0, 0);
          $purple = ImageColorAllocate($im1, 255, 0, 255);

          $im=imageCreateFromJpeg("bgregsmiling.jpg");

          imagecopy($im1, $im, 0, 0, 0, 0, $w, $h);
          $font="/home/virtual/site2/fst/var/www/html/fonts/ALPHA.TTF";

          imageTTFtext($im1,50,0,20,100,$black,$font,"Greg");
          imageTTFtext($im1,50,0,20,200,$white,$font,"Greg");
          imageTTFtext($im1,50,0,20,300,$purple,$font,"Greg");

          imagejpeg($im1);
          imageDestroy($im);
          imageDestroy($im1);
          ?>

            Write a Reply...