Jump to content

[SOLVED] imagettftext() does not use the color I specify


sneamia

Recommended Posts

<?
$img = imagecreatefromgif('resources/sig_bg.gif');

$background = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);

$bbox = imagettfbbox(16, 0, 'resources/CALIBRIB.TTF', 'wrong');
imagettftext($img, 16, 0, 455 - $bbox[2], 70, $white, 'resources/CALIBRIB.TTF', 'wrong');

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>

 

As you can see, I open an image stored locally on my server.  It then uses imagettftext to write white text on the image.  However, this is where it screws up.  The text it writes is black, not white.  I'm not entirely sure what I am doing wrong.  Any ideas?

 

I tried $img = imagecreate(500, 100); and it writes the text correctly, with the correct color.  It seems like there is something incompatible when I use imagecreatefromgif and imagettftext.

Try now dude this will work

 

<?php
$img = imagecreatefromgif('resources/sig_bg.gif');

$background = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);

$bbox = imagettfbbox(16, 0, 'resources/CALIBRIB.TTF', 'wrong');
imagettftext($img, 16, 0, 455 - $bbox[2], 70, $white, 'resources/CALIBRIB.TTF', 'wrong');

header('Content-type: image/png');
imagegif($img);
imagedestroy($img);
?>

 

Strange one. I had to use my own gif and font (arial) but your code worked fine - white text ??? ???

 

 

@d.shankar

 

GIF was converted to GD image format

GD image was converted to PNG on output.

 

Nothing wrong with that. In fact that's how to convert a file from one format to another.

 

I agree; very strange. My only thought is that it might be something to do with the fact that gifs are limited to 256 colours. I'm not sure if that could possibly cause a problem though.

 

I wonder if you could perhaps supply the image you're working with so I could test it with that. Also, what version of PHP and GD are you using?

(Just had same thought, GR)

 

I wonder if it's a pallette issue.

 

Check the value of $white. If it's -1 the allocation failed. (Full pallette?)

 

In that case try

 

$white = imagecolorexact($img, 255,255,255) // if it's already in pallette

or

$white = imagecolorclosest($img, 255,255,255) // get nearest to white from pallette

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.