
People have been trying to convert a text to an image for various reason. They usually need it to turn an e-mail address so it cannot be easily harvested from the content. But this script will not only able to do that, it can be use for many other thing, such as styling your website. As we notice, there are many True-type font we want to use for better site typography like using it on a document heading or else. The problem is, not many people have the font we want. So, how can we solve that? One of the solution is using an image, and this tutorial will explain how to convert any text to image on your website using PHP.
Script to Convert Text to Image
First, we need to create a PHP file to write the script. At the very begining of the script. we need to configure how we want the image to be generated by simply write this code:
<?php $font_size = 32px; // The generated size (in pixel) of the font $font_color = '000000'; // The generated color of the font (in HEX) $font = 'arena-condensed.ttf'; // Font file we need to use $text = 'PHP GD Text to Image example'; // Text we need to convert ?>
Before we generate the image, we should convert the color into Hexadecimal. Add this below the above script:
<?php list($r, $g, $b) = array( $font_color[0].$font_color[1], $font_color[2].$font_color[3], $font_color[4].$font_color[5] ); $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); $color = array( $r, $g, $b ); ?>
Now that we get all the parameter we need, the next step is the conversion. Add this below the previous script:
<?php
// Get the image size
$size = @imagettfbbox($font_size,0,$font,$text);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image = @imagecreatetruecolor($xsize+5, $ysize);
// Set the image transparency
@imagesavealpha($image, true);
$transparent = @imagecolorallocatealpha($image,0,0,0,127);
@imagefill($image,0,0,$transparent);
// Coloring the text
$font_color = @imagecolorallocate($image, $color[0], $color[1], $color[2]);
@imagettftext($image,$font_size,0,0,abs($size[5]),$font_color,$font,$text);
// Return the image to the browser
header("Content-Type: image/png");
@imagepng($image);
@imagedestroy($image);
ob_flush();
?>
We’re done! Wrap up all the code in a file and upload it to your server. Remember to upload the font you are going to use (in this case, it should be named arena-condensed.ttf ) in the same directory with the script. Call the file on your browser to see the result, it should be something like this:
Click here for more information about PHP GD



5 comments on "Convert Text to Image Using PHP"
nice, however, it usually used for captcha.
i have the shorter code on http://mr.hokya.com/?s=captcha
Yes, that can be shorten that way. But what I did here was trying to create a dynamic image, which color can be specified and the dimension can be dynamically defined from the font you used and the size of that particular font. It’s also create a transparent image.
read http://arie.putranto.com/blog/2009/06/php-captcha-image-verification/ on how to generate CAPTCHA.
It would be nice to also do a cashing of the generated images — GD is pretty slow operation, so on the busy site it ma take a while if you generate a lot of these images on the fly.
This could be a good technique for protecting email addresses from spam bots for instance.
I’ve also seen people using the similar trick to generate the pretty menu items for their sites.
Would be nice to make it into a standalone class and into a WordPress plugin for instance.
Yes, i thought so. I’m in the middle of developing new wordpress theme framework, and this function is included. And sure, it would also do caching. A WordPress plugin will be good, I will working on that. Thanks for reading and commenting
Hello,
I have tried the above code for converting text to image, but the page just end-up displaying some funny looking codes/text characters.
Regards
2 trackbacks
Convert Text to Image Using PHP | Arie Putranto…
People have been trying to convert a text to an image for various reason. They usually need it to turn an e-mail address so it cannot be easily harvested from the content. But this script will not only able to do that, it can be use for many other thin…
Convert Text to Image Using PHP…
People have been trying to convert a text to an image for various reason. They usually need it to turn an e-mail address so it cannot be easily harvested from the content. But this script will not only able to do that, it can be use for many other thin…