one problem generating transparent png text.

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
destrella
Forum Newbie
Posts: 3
Joined: Sun Nov 09, 2008 10:14 am
Location: Mérida, Yucatán, México

one problem generating transparent png text.

Post by destrella »

Hello to all in this community!
Based on info found from several sources, i wrote this little script to write transparent images with a specific font:

Code: Select all

<?
header ("Content-type: image/png");
$Fuente = "english.ttf";
$r=255;
$g=153;
$b=255;
$Dimension = imageTTFBBox($Tamanio, 0, $Fuente, $Texto);
$Imagen = imageCreateTrueColor(abs($Dimension[2]) + abs($Dimension[0]), abs($Dimension[7]) + abs($Dimension[1]));
imageSaveAlpha($Imagen, true);
ImageAlphaBlending($Imagen, false);
$Transparencia = imagecolorallocatealpha($Imagen, $r, $g, $b, 127);
imagefill($Imagen, 0, 0, $Transparencia);
$Color = imagecolorallocate($Imagen, $r, $g, $b);
imagettftext($Imagen, $Tamanio, 0, 0, abs($Dimension[5]), $Color, $Fuente, $Texto);
imagepng($Imagen);
imagedestroy($Imagen);
?>
The script works like a charm, but it has one issue with italic or handwritten fonts; the resulting png has a transparent background, but it seems that at the moment to write each letter, it writes it erasing all what is behind the white space of the letter, generatin a blank square deleting all what is behind. I dont know if im explaining well, but here is and example, this is my working script with a handwritting font:
http://roselly.destrella.com.mx/wp-cont ... qrstuvwxyz

but it should work like this (this sample is from the dafont.com website where i've downloaded the font):
http://img.dafont.com/preview.php?text= ... ize=m&y=54
(copy & paste the dafont.com link if doesnt work just clicking on it)

as you can see, both scripts works with the same font but something seems to be missing on mine; so the question here is, how to make it work properly? where is the mistake and how to solve it?

thanks in advance for your time and help :)
BinaryPusher
Forum Newbie
Posts: 2
Joined: Thu Dec 04, 2008 8:53 pm

Re: one problem generating transparent png text.

Post by BinaryPusher »

First off, sorry I don't have a solution, i have the same problem. If I find one I'll post it but I was hoping you'd found a solution else where. The best i've found is a posting on the php bugs site but the developer responsible refuses to recognise anything beyond its a fault with the font file. I've testing with several of my fonts and all of them produce the same blockyness. I find it hard to beleive all the fonts are broken/faulty.

Can you tell me if you've gotten anywhere with this?

Steve
User avatar
destrella
Forum Newbie
Posts: 3
Joined: Sun Nov 09, 2008 10:14 am
Location: Mérida, Yucatán, México

Re: one problem generating transparent png text.

Post by destrella »

Hello Steve, so far i havent found a solution, one way to solve it, maybe could be to write the letters one by one in different files, and then put it all together. Thats just and idea i had but i still havent tried partly cause i've been busy in other projects for the office.. and this one is just for a friend :P
If you try this suggestion and succeed, please let me know :D

Cheers!
BinaryPusher
Forum Newbie
Posts: 2
Joined: Thu Dec 04, 2008 8:53 pm

Re: one problem generating transparent png text.

Post by BinaryPusher »

Hi mate,

I had the same idea too. spent the whole day brute force bashing it into submission. Only now at 5:15 did I realise the problem. and it's sooooo simple I can't believe I still make mistakes like this.

the problem is:
# ImageAlphaBlending($Imagen, false);

change this to:
# ImageAlphaBlending($Imagen, true);

and you'll be laughing.
User avatar
destrella
Forum Newbie
Posts: 3
Joined: Sun Nov 09, 2008 10:14 am
Location: Mérida, Yucatán, México

Re: one problem generating transparent png text.

Post by destrella »

OMG thats true! 8O
Thank you for the help Steve!!
Post Reply