Search code examples
phpgdimagettftext

PHP imagettftext always drawing black


Basically when I draw text it's ending up black like this: https://i.sstatic.net/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.


Solution

  • 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);