4

Basically when I draw text it's ending up black like this: https://i.stack.imgur.com/z675F.png Instead of the color I'm allocated in PHP and the function. Code:

    $finalImage = imagecreatefrompng($imageFile);
    $logo = imagecreatefrompng($logoImage);
    imagecopy($finalImage, $logo, $logoPosition['x'], $logoPosition['y'], 0, 0, imagesx($logo), imagesy($logo));
    $font = "arial.ttf";
    $fontSize = 10;
    $yOffSet = 15;
    $white = imagecolorallocate($finalImage, 255, 255, 255);
    foreach($pixelArray as $key => $x) {
        foreach($valueArray[$key] as $valueText) {

            imagettftext($finalImage, $fontSize, 0, $x, $yOffSet, $white, $font, $valueText);
            $yOffSet += 15;
        }
        $yOffSet = 15;
    }
    if($miscText != null) {
        foreach($miscText as $key => $text) {
            imagettftext($finalImage, $fontSize, 0, $text['x'], $text['y'], $white, $font, $text['text']);    
        }
    }
    imagepng($finalImage,$saveFileName.".png");
    imagedestroy($finalImage);

It was working before, but then it just stopped and I have no clue why. It was after I changed the source image (Was generating fine) and I hadn't touched the code. I've tried all sorts of things with changing the colors, but I can't get it to display in anything other then black.

UberMouse
  • 917
  • 2
  • 12
  • 26

3 Answers3

5

Fixed it by changing imagecolorallocate to imagecolorclosest since I already some white text on a logo I copy in:

//       imagecolorallocate....
$white = imagecolorclosest($im, 255, 255, 255); 
T.Todua
  • 53,146
  • 19
  • 236
  • 237
UberMouse
  • 917
  • 2
  • 12
  • 26
3

Have you checked if the imagecolorallocate() function is returning boolean false, as it will if the allocation fails? If the $finalImage .png is 8bit, and your pure white color isn't in the source image's palette, this call will fail. You do say you changed the source image, so this is most likely why it's broken now.

$white = imagecolorallocate($finalImage, 255, 255, 255);
if ($white === FALSE) { // note the === -> strict type comparison
    die("Failed to allocate color 255/255/255")
}

The function will also simply return a number representing the color triplets, in this case it would 0xFFFFFF. You can try passing that into the imagegetttftext() call directly, see if that helps.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I did try using 0xFFFFFF and it just stopped the text from displaying entirely. It did not occur to me to check the allocation limit so I will check that. I'm also fairly sure it's not the image, I tried painting the background black again and it still had the issues. Ok, yes it is failing to allocate. I have a logo I write to the image which contains white text, that displays correctly. Is there any way I can use that white? – UberMouse Dec 20 '10 at 14:15
  • If the palette's full, and doesn't already contain a pure white, then you'd have to `imagecolordeallocate()` (remove) one of the existing colors to free up a slot for the pure white. – Marc B Dec 20 '10 at 14:39
0

Use imagecreatetruecolor() to allocate the color:

$width = 500; //whatever width you need
$height = 200; //whatever height you need
$white = imagecolorallocate(imagecreatetruecolor($width, $height), 255, 255, 255);

That worked for me.

joan16v
  • 5,055
  • 4
  • 49
  • 49