Generate text with transparent background
Sometimes it’s necessary to generate images with text on, and it can be a wise idea to generate it with transparent background so you can use it on different colors.
First some variables to define how the function will work.
$theText is the text that we want to have in the image.
The other variables should also be pretty easy to understand…
I defined them in variables so you easily can change them to try different things.
The first thing which is a little bit tricky is the “imageTTFBBox”-function.
Not really self explaining hehe ![]()
What it does is depending on the parameters it gives back the size of the bounding box needed for this text, in pixels.
You get an array with this information:
0 lower left corner, X position
1 lower left corner, Y position
2 lower right corner, X position
3 lower right corner, Y position
4 upper right corner, X position
5 upper right corner, Y position
6 upper left corner, X position
7 upper left corner, Y position
The next step is to create the image (empty but with correct size to fit the text) with this function:
imageCreateTrueColor(x, y)
You give the function the width in x and y, and you get the values you see in the function call.
imageSaveAlpha sets the mode to save all alpha channel information, which is needed to make a png file transparent.
ImageAlphaBlending sets the blending mode off, which meens “simply” that the alpha information is preserved. (not that important, just set it to false hehe)
imagecolorallocatealpha allocates a color for an image, and sets how transparent it should be. 0 is NO transparency and 127 is fully transparent.
127 is prefect for us, but you can always mess around with the numbers to se what happens ![]()
This color variable is used for filling the “real” image with transparent color.
The last part is pretty straight forward.
Get textColor, and write the text in the image. After that we just save it with the name textImage.png.
OBS, the web server needs write permissions to this catalog.
So if you are going to use this in a live application/site, you should put the images in a separate directory and change permissions on that.
$theText = "999 Tutorials, transparent text"; $fontSize = 15; $angle = 25; $font = "arial.ttf"; $size = imageTTFBBox($fontSize, $angle, $fontSize, $theText); $image = imageCreateTrueColor(abs($size[2]) + abs($size[0]), abs($size[7]) + abs($size[1])); imageSaveAlpha($image, true); ImageAlphaBlending($image, false); $transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127); imagefill($image, 0, 0, $transparentColor); $textColor = imagecolorallocate($image, 200, 200, 200); imagettftext($image, $fontSize, 0, 0, abs($size[5]), $textColor, $font, $theText); imagepng($image, "textImage.png"); imagedestroy($image);
If you think that the code is hard to understand, just read the text once again. If you still don’t understand…. don’t care
Just use it and get a beer instead!

May 8th, 2009 at 3:57 am
Kindly elaborate & please use screen shots cz its really confusing
August 5th, 2009 at 11:25 pm
Hi, It would be really nice if you can provide a ample cause i just cant get the code to work! i am getting the following errors:
Warning: imagettfbbox() [function.imagettfbbox]: Could not find/open font in D:\wamp\www\temp\temp.php on line 7
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in D:\wamp\www\temp\temp.php on line 8
Warning: imagesavealpha(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 9
Warning: imagealphablending(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 10
Warning: imagecolorallocatealpha(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 12
Warning: imagefill(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 13
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 15
Warning: imagettftext() expects parameter 1 to be resource, boolean given in D:\wamp\www\temp\temp.php on line 16
Warning: imagepng(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 17
Warning: imagedestroy(): supplied argument is not a valid Image resource in D:\wamp\www\temp\temp.php on line 18
Thanks,
Fresh Web Developer
August 11th, 2009 at 7:23 am
Not really sure, but I think you should read this post: http://jamazon.co.uk/web/2008/06/04/imagettfbbox-could-not-findopen-font/
September 23rd, 2009 at 6:00 pm
you define $font on line 4 yet call for $theFont on line 6 and then call for $font on line $15…
September 27th, 2009 at 7:09 am
Thanks for pointing it out! I think I have corrected it now!
November 4th, 2009 at 4:32 am
This code should work for anybody having difficulties
November 11th, 2009 at 3:48 am
No correct. Still don’t work.
Maybe you can´t use double same parameters $fontSize in line 6.
)
Bud i don´t know what i must use
December 10th, 2009 at 9:00 am
Great! Just what I`ve been looking for
Used just 4 lines of code, but its great anyway hehe.
December 10th, 2009 at 9:21 am
Guess that is because of the error on row 7. Probably you don’t have the font file arial.ttf. I have not tried this code on windows, but on a linux system you usually have the fonts here: /usr/share/fonts/ or maybe in the same directory as your script.
You could try to not specify what font to use, don’t know how it’s handled but worth a try.
January 7th, 2010 at 11:22 am
Hi,
Thanks very much for the tip !
For people that can’t make that script working : it’s normal.
1) There is an error Line 6 :
replace
$size = imageTTFBBox($fontSize, $angle, $fontSize, $theText);
by
$size = imageTTFBBox($fontSize, $angle, $font, $theText);
2) line 4 : you have to put the absolute path to your arial.ttf file :
$font = “/your_path/arial.ttf”;
Regards,
Damien
January 11th, 2010 at 1:00 am
Thanks for this one. It’s useful. I did notice one bug, or I believe a bug in it.
This line:
$size = imageTTFBBox($fontSize, $angle, $fontSize, $theText);
When this was as it is it didn’t work but I changed it too:
$size = imageTTFBBox($fontSize, $angle, $font, $theText);
That fixed it.
January 12th, 2010 at 1:36 pm
the size line refers to fontsize twice when the secod reference should be the font not the fontsize