Text to Image converter php script Protect Email ID

Hi there, I did this project 1 year back and the first article about this script was in bangle. Today I will write that into English.  I prepared that script as I have a site where I need to share email ID. But text email id is very attractive element for the email marketer! They just search @ sign and grab email ids! That's why I thought I need email id as an image.


Anyway, let's began. What I am gonna say is about a php script that will convert text into image and you may use it to print email on your site same like me. Well, Demo and Download link of the script is as follows


Text to Image php Script Demo and Download Page

Now we will see the codes step by step. First create a new php file on your server give the name image.php, you are free to chose other name also. It's totally up to you. And on that new file, copy the following code

<?php
if(!isset($_GET['img']))
{
exit();
}

header ("Content-type: image/png");
$string = $_GET['img'];                                            
$font   = 4;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font)+2;
$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);
imagepng ($im);
?>

The above line of code will get text in get method and convert in into Image. And now open another  file (may be index.php or txt2img.php ) and put the below code.


<?php $txt="emailID@example.com"; ?>
<img src="image.php?img=<?php echo $txt; ?>" border="0">

Now open this page in a browser and see what happens. It will show an image of emailID@example.com ! But, there is a but! if you see  the source code, there is still the @ sign and the hole email id! So, how we will remove that @ sign? Well, there is also way, we gonna use a function that will remove @ sign from the email id.

See the php code below, we created a function named convATandDot , this function will remove the @ and . sign.

<?php
function convATandDot($currenttext){
$old = array('@','.');
$new = array('__shariar__','__dot__');
$convTEXT= str_replace($old, $new, $currenttext);
return $convTEXT;
}
?>

The function convATandDot will get the email id or any text and then convert @ into __shariar__  and . into __dot__ ! For the email Id emailID@example.com, the function will return emailID__shariar__example__dot__com

So let's what is the final version of the script.


Make the first file (image.php) like below

<?php
if(!isset($_GET['img']))
{
exit();
}

header ("Content-type: image/png");
$string = $_GET['img'];  
$string = convATandDot($string);                                          
$font   = 4;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font)+2;
$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);
imagepng ($im);

function convATandDot($currenttext){
$old = array('__shariar__','__dot__');
$new = array('@','.');
$convTEXT= str_replace($old, $new, $currenttext);
return $convTEXT;
}
?>

And the other file will be as follows

<?php $txt="emailID@example.com"; ?>
<img src="image.php?img=<?php echo convATandDot($txt); ?>" border="0">
<?php
function convATandDot($currenttext){
$old = array('@','.');
$new = array('__shariar__','__dot__');
$convTEXT= str_replace($old, $new, $currenttext);
return $convTEXT;
}
?>

Test and download the script @ Text to Image php Script Demo and Download Page. When you will finish scripting, test with text emailID@example.com and it will return an image like below

Email ID as Image
Email ID as Image

So, there you are. Enjoy the tutorial. I hope with in few days I will give you another script for htaccess to control the Image url. Because the current script return the php extension of the image file. But using htaccess we can make it with file extension png! See you soon...

Related Post

Split domain name into sld and tld – php script A series of alphanumeric strings that are separated by periods, such as www.shariarbd.com, that is an address of a computer network connection and tha...
Bootstrap Responsive hexagon Today we will see how to create a Responsive Hexagon that will adjust automatically according to its outer container. Actually we will use bootstrap g...
Responsive isotope version 2 masonry layout Isotope is a nice script to organize and filter your portfolio or gallery item with some cool effects and functionality like masonry  layout support. ...
Create CSS3 Hexagon There are two easy way to make CSS3 Hexagon  and I am gonna show you the way in details that how to create css3 hexagon. In both case we will use ':af...

Leave a Reply

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

*