Php imagettftext always draws black

Basically, when I draw text, it ends in black as follows: http://i.stack.imgur.com/z675F.png Instead of color, I stand out in PHP and functions. The 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);

He worked before, but then he just stopped, and I don’t know why. This was after I changed the original image (created beautiful), and I did not touch the code. I tried all kinds of things with changing colors, but I can not make it appear in anything other than black.

+3
source share
3 answers

, imagecolorallocate() false, , ? $finalImage.png 8 , , . , , , , .

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

, , 0xFFFFFF. imagegetttftext(), , .

+3

, imagecolorallocate imagecolorclosest, , .

+1

imagecreatetruecolor(), :

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

.

0

Source: https://habr.com/ru/post/1781235/


All Articles