PHP Tutorial : Write text and convert into image

The base usage in my mind when I write this tutorial to convert text into image with php is to write email into images dinamically to avoid spammers collecting such addresses from the internet.

To run such a script you must have GD library installed. We’ll take parametres via GET and merge into 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 and read text do it like :

<img src="textToImage.php?text=emailAddress@emailProvider.com">
This entry was posted in Usefull PHP and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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