Create image with PHP / GD library, And Adding Fonts
GD Library is one of the cool features started from PHP 3.0 , It`s helps create image on the fly , It just needs PHP and GD library Installed on the server.
~ It`s Just Creation of images from PHP ~
Using this we are creating a simple PNG image without any source image . just pure PHP :) .
< ?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
header(”Content-type: image/png”); - This defines Image Extention, as here it is PNG
$string = $_GET['text']; - This is to get the text from the Input page
$im = imagecreatefrompng(”images/button1.png”); - This is the location of the source image.
$orange = imagecolorallocate($im, 220, 210, 60); - This defines the colour.
$px = (imagesx($im) - 7.5 * strlen($string)) / 2; - This figures out the Size.
imagestring($im, 3, $px, 9, $string, $orange); - This Combines all the the above defined .
imagepng($im); - Now it Creates the Image
imagedestroy($im); - Once the image is created it is only shown for that session time after that it is deleted
This example would be called from a page with a tag like:
< img src=”button.php?text=text”/>. The above (say) button.php script then takes this “text” string and overlays it on top of a base image which in this case is “images/button1.png” and outputs the resulting image. This is a very convenient way to avoid having to draw new button images every time you want to change the text of a button. With this method they are dynamically generated.
To Add Fonts,
Add some fonts in the same directory of the script , and then add $font= “somefont.ttf” in the script file. this has to be called in the URL i.e button.php?text=text&font=arial.ttf and then add it with the imagecreate line.
There is another Tutorial based on this ,which shows the users IP address and act as a Hit counter. You can see the tutorial here
You can learn a whole lot from PHP`s Official Site .
Make a image like this with your name Online
Here

April 22nd, 2005 at 10:46 pm
Cool Work ! Keep it up .
July 12th, 2005 at 10:10 am
How to change the Colour of the fonts ?
July 12th, 2005 at 11:28 am
Using function imagecolorallocate
for e.g :
gives red
GIves Dark Green
Gives Brown
You can refer here for more
August 12th, 2005 at 9:09 am
Thanks , Worked !
August 15th, 2005 at 8:01 am
Sweet :) thnx
October 31st, 2005 at 5:30 pm
Nice tutorial!
I always wondered how this is done… Is this the technique used for making ‘anti-bot images’ eg on form submission?
December 23rd, 2005 at 9:07 pm
Yes, Usually it is ! but it can be done with other programming languages such as Perl too. :)
February 11th, 2006 at 11:15 pm
[...] GD Library is one of the cool features started from PHP 3.0 , It`s helps create image on the fly , It just needs PHP and GD library Installed on the server. ~ It`s Just Creation of images from PHP ~ (more…) [...]
September 28th, 2006 at 3:33 pm
I got everything to work the way I want it to, but now I’m still stuck with one question… How can I assign the text to a certain size?
Either way, thanks a bunch for these tutorials, they’re really helping me and a whole lot of other people out a lot.
October 4th, 2006 at 5:48 am
I seem to have been mislead…
December 10th, 2006 at 10:55 pm
Does anyone know if you could use this in conjunction with Sean Inman’s Flash image replace ideas? (http://www.mikeindustries.com/sifr/) He’s written a script that will replace all H1, H2 (etc) tags with Flash files containing an embedded font of the original content of those tags. It’s nice because the text remains selectable (and therefore copy-and-pasteable) and the font is completely custom, but I’d really like the flexibility of using a simple image instead of a Flash file with an embed tag and all that stuff. Any ideas?
September 7th, 2007 at 11:33 pm
What are the fonts that can be used in making images??
October 29th, 2008 at 7:56 pm
It says we need no image. when we have a source image:
And there is the image we need for the background!!!