UberMouse UberMouse - 1 year ago 85
PHP Question

PHP imagettftext always drawing black

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

$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']);

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download