overshadow
overshadow

Reputation: 988

Php GD add text to png image always follow image background color

When adding text to a png image using gd library the text always follows the image background color even I have set the color using the imagecolorallocate(), why is this?

This is my code:

<?php
header ('Content-Type: image/png');

$im = imagecreatefrompng('picture.png');

$text_color = imagecolorallocate($im, 233, 14, 91);
$text = 'A Simple Text String';
$font_path = './font/arial.ttf';

imagettftext($im, 16, 0, 100, 200, $text_color, $font_path, $text);

imagepng($im);
imagedestroy($im);
?>

Upvotes: 0

Views: 1507

Answers (1)

cleong
cleong

Reputation: 7646

If you're loading an 8-bit PNG file, chances are that all 256 entries in the color palette have already been allocated. Your call to imagecolorallocate() will return false. That gets converted to 0, which presumably is the index of the background color.

What you should do is call imagecolorstotal() to see if it's less than 256. If the palette is full, convert the image to true color before proceeding.

Upvotes: 2

Related Questions