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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.