Basic idea behind converting text to image is to avoid spammers grabbing important data like emails, phone numbers etc. This simple php script turns given text to image dynamically.

<?php
header ("Content-type: image/png");
$text='test@example.com';
$string = $text;                                            
$font   = 3;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
 
$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0,  $string, $text_color);
echo imagepng ($im);
?>


Sample Usage:

This example consists two parts,
  • index.php which displays contents including user emails
  • tx2img.php receives key and outputs email images related to given key

index.php
This index.php stores name & emails in a session variable and sends name as a parameter to txt2img.php


<?php
@session_start();
$_SESSION['images']=array('john'=>'John@sample.com','peter'=>'Peter@example.com');
 
echo "This sample illustrates how to convert text into images using php<br><br>";
echo "First Email:<img src='http://localhost/txt2img.php?img=john' /><br>";
echo "Second Email:<img src='http://localhost/txt2img.php?img=peter' />";
?>

txt2img.php
This page receives name as a parameter then outputs related emails as image


<?php
ob_start();
@session_start();
header ("Content-type: image/png");
$text=$_SESSION['images'][$_GET['img']];
if($text=='')
 exit;
 
$string = $text;                                            
$font   = 3;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
 
$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0,  $string, $text_color);
echo imagepng ($im);
?>

When you run index.php in browser, you’ll get output similar to the one given below:
convert text to image output.png

 


Comments (0)
Leave a Comment

loader Posting your comment...