The following code contains a function which uses the gd library in php to convert a given text into an image. This was needed in order to prevent bots from reading email ids displayed on a website. There are a lot of scripts available readily which will display an image when the text is passed as a get variable to the php file. I was unable to use this because there was no point including the email id as get variable, making it simple for a spam bot to gather information.
The tricky part is to capture the output of the function imagegif( ) so that it does not hit the browser screen.
A more readable code
<?php
function convert_text_image($text_input)
{
$my_img = imagecreate( 290, 18 );
$background = imagecolorallocate( $my_img, 255, 255, 255 );
$text_colour = imagecolorallocate( $my_img, 0, 0, 0 );
imagestring( $my_img, 4, 0, 0, $text_input,$text_colour );
ob_start(); // This prevents the imagegif function from outputting to the browser.
imagegif($my_img);
$img_out = ob_get_contents(); // This gets the raw data of the image.
ob_end_clean(); // Clear all output till now
$img_out = chunk_split(base64_encode($img_out)); // Encodes the data so that we can display this as a raw image on the browser.
$string = "\"data: image/gif;base64,".$img_out."\"";
echo "";
imagedestroy( $my_img ); //Free resources.
}
$text = "example@example.com";
convert_text_image($text);
?>