Image titles generated with PHP and GD
Saturday, July 21st, 2007 in PHP
Some time ago, a client came and asked specifically that the headers (h1 stuff) should use his particular font specified in his identity manual. At that time I did now knew about the flash possibility to overwrite text (search for sIFR.js and you’ll see what I mean), so I chose to dynamically generate these titles using PHP and GD. The reason for this solution (instead of changing them using some image editing software) was that there was some dynamic data (a list of newsletters) that needed the same title styling.
This is the story of this one client. Over the time I heard this request from my clients a little too often, despite the fact that I told them it will harm the SEO process. All they knew was that those darn titles MUST use their fonts. End of story.
So, I had to find an easy solution. The solution I developed is a very simple, very small function that uses the GD functions to generate the images using some true type font specified by the client.
Here goes the code of that function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <? function create_image($image_text,$image_path) { /* we determine the length of our image_text in order to calculate the width of the image */ $word_length = strlen($image_text); $image_width = $word_length*18+10; $image_height =20; $img = imagecreate($image_width,$image_height); /* we define the colors in our image by using the RGB color code */ $white = imagecolorallocate($img, 255, 255, 255) ; $image_text_color = imagecolorallocate($img, 53, 69, 170) ; /* fill the image with the background color, in our case it's white */ imagefill($img, $image_width, $image_height, $white); /* we add the image_text to our image */ imagettftext($img,12,0,15,15,$image_text_color,"fonts/dodg5.ttf",$image_text); /* in order to make a direct output instead of storing the image on the hard disk, you must uncomment the 3 lines below and comment the last line - meaning imagejpeg($img,$cale); */ //header("Content-type:image/jpeg"); //header("Content-Disposition:inline ; filename=secure.jpg"); //imagejpeg($img); imagejpeg($img,$image_path); } ?> |
Some things you might be interested in:
Giving the fact that the image with is calculated based on the length of the input text and that letters have different width and some font are wider or narrower, you might need to tweak the line
1 | $image_width = $word_length*18+10; |
into something like
1 | $image_width = $word_length*15+10; |
if you are using a font that is narrow.
Download files
Download the PHP-GD image titles script. Have fun!






1 comment
its really helpful to start php images
Leave a comment