Dynamically create an image from any text in PHP

My goal when I did this quick script was to use php to convert text into images, so that email addresses could be dynamically inserted into pages, making spam harvesters’ life a little harder.

Of course it has many more uses, one of them being to avoid web spiders from fetching a specific word or two -say a promo code you don’t want to be indexed.

To run this script you must have GD library installed. We’ll take parameters via GET and put that text into the image:

<?php

if(!isset($_GET['text']))
{
die("No text provided");
}
header ("Content-type: image/png");
$textToConvert = $_GET['text'];
$font   = 4;
$width  = ImageFontWidth($font) * strlen($textToConvert);
$height = ImageFontHeight($font);
$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //this means it's white bg
$text_color = imagecolorallocate ($im, 0, 0,0);//and of course black text
imagestring ($im, $font, 0, 0,  $textToConvert, $text_color);
imagepng ($im);
?>

Save this as texttoimage.php and then when you want to create the image, link to it in an ‘img’ tag.

<img src="texttoimage.php?text=emailAddress@emailProvider.com">

Hey, be warned, the above won’t stop spammers and spiders from fetching your email address! They’ll see and properly parse the query string -this is for demonstration purposes only.

4 Responses to “Dynamically create an image from any text in PHP”

  1. Deb Says:

    Neat!


  2. Sam Says:

    wow, didn’t think it could be this simple! You ROCK!!!!


  3. srisoftwarez Says:

    great work.. if i provide text value automatically then how can i call this image..


  4. vishnu Says:

    great man


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>