While building web content we may need to include email address. In this case spam bots are may access our web page and collect email address inside our web content. based on this
email address extracting script.
The only way to protect Email address inside the content is convert all email address into image.
Here is the solution to convert email address into dynamic text images
email-to-image.php
<?php
define("F_SIZE", 10);
define("F_ANGLE", 0);
define("F_FONT", "verdana.ttf");
$text = base64_decode(base64_decode($_GET['id']));
$Leading=0;
$W=0;
$H=0;
$X=0;
$Y=0;
$_bx = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$s = preg_split("/\n]+/", $text); // Array of lines
$nL = count($s); // Number of lines
$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+2+($nL>1?($nL*$Leading):0):$H;
$img = @imagecreate($W+8, $H) or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($img, 255,255,255);
$txtColor = imagecolorallocate($img, 255,50,0);
// Adjust padding right:
$W+=8;
$bgwhite = imagecolorallocatealpha($img,238,238,238,0);
imagefilledrectangle($img, 0, 0,$W,$H, $bgwhite);
$alpha = "".range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);
// Use the string to determine the height of a line
$_b = imageTTFBbox(F_SIZE, F_ANGLE, F_FONT, $alpha);
$_H = abs($_b[5]-$_b[3]);
$__H = 0;
// Use the string to determine the width of a line
$_b = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$_W = abs($_b[2]-$_b[0]);
// Final width and height
$_X = abs($W/2)-abs($_W/2);
$__H += $_H;
imagettftext($img, F_SIZE, F_ANGLE, $_X, $__H, $txtColor, F_FONT, $text);
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
// END EMAIL IMAGE
exit();
?>
index.php
<?php
if(isset($_POST['content']))
{
$content=$_POST['content'];
// Keep line breaks inside textarea
$content=preg_replace("/[\n]/", '<br />', $content);
// Find All email ids inside content
preg_match_all('/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/',$content,$emails);
// Replace as image every email address
foreach($emails[0] as $email)
$content=str_replace($email,'<img src="email-to-image.php?id='.base64_encode(base64_encode($email)).'">',$content);
}
?>
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
body,td,th {
font-family: Verdana, Geneva, sans-serif;
font-size: 12px;
color: #000;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 5px;
margin-bottom: 5px;
margin:0 auto;
width:750px;
}
textarea { width:750px; font-size:15px; font-family:Verdana, Geneva, sans-serif;}
#content
{
font-size:13px;
padding:10px;
background:#EEE;
border:1px solid #CCC;
}
#content img
{
position:relative;
margin-bottom:-3px;
}
</style>
</head>
<body>
<br>
<br>
<h1>Protect Email Address inside web content</h1>
<form action="" method="post">
<textarea cols="70" rows="10" name="content">
content mixed with any email address. that email address text will be converted into as image. for example we have info@example.com mail in this text area it will be return as image after the compilation. any no of occurrence will automatically replaced as image
For Example:
Contact
admin@example.com
Technical
tech@example.com
</textarea>
<br /><br />
<input type="submit" value="Compile" name="submit" /><br />
<br />
</form>
<div id="content">
<?php echo @$content; ?>
</div>
</body>
</html>