Display "anti-spam" email adress as image using PHP

Posted in "News"

image 2 image 1

This trick is already widely known among developers,  but still I wanted to share my script with you all. The concept is simple, use PHP and GD-library to show a email address in a web-page as an image. This way spam bots aren't able to pick up the email address, they scan all web pages for obvious elements that are related to an email address <user>@<domain>.<ext>


Most spam bots are only able to read HTML code, however, this method isn't waterproof either. Some bots are capable of reading images...

Anyway, this is still helpfull though! Another  and probably best option is to use a contact form, but for those who need to show multiple email addresses on their web page, here's how:

Create a new php page  named "email.php" and paste the code below inside the new page.


*    Convert email addresses to an image dynamically    *

//-------------------- Configuration --------------------
$fontgroote = 3;

//Font-color RGB 255,255,255 = wit
$RGBfont = array("R" => 255, "G" => 255, "B" => 255);

//Background-color: RGB 0,102,0 = groen
$RGBbg = array("R" => 0, "G" => 102, "B" => 0);

/* ---- Do not edit below  (unless you know what you're doing---- */
if (isset($_GET["mg"]) and !empty($_GET["mg"]) and isset($_GET["md"]) and !empty($_GET["md"]) and isset($_GET["mext"]) and !empty($_GET["mext"])) {
 //Setup the emailaddress
 $tekst = $_GET["mg"]."@".$_GET["md"].".".$_GET["mext"];
} elseif (!isset($_GET["mg"]) or (empty($_GET["mg"]))) {
 //No user was given in the URL
 $tekst = "No user given";
} elseif (!isset($_GET["md"]) or (empty($_GET["md"]))) {
 //No domain name was passed via the URL
 $tekst = "No domain given";
} elseif ((!isset($_GET["mext"])) or (empty($_GET["mext"]))) {
 //No extension passed in the URL (.be, .com,...)
 $tekst = "No domain extension given (.com, .net, .org,...)";
} else {
 //Another error occured
 $tekst = "Unknown error";

//Calculate the width and height according to the fontsize
$breedte  = ImageFontWidth($fontgroote) * strlen($tekst) + 6;
$hoogte = ImageFontHeight($fontgroote) + 6;

//Draw the image using given measurements (width and height)
$im = imagecreate($breedte,$hoogte);

//Select wich color pallet we will use (RED background when an error occurs)
if (isset($_GET["mg"]) and !empty($_GET["mg"]) and isset($_GET["md"]) and !empty($_GET["md"]) and isset($_GET["mext"]) and !empty($_GET["mext"])) {
//background and font color in RGB values
$bg = imagecolorallocate($im, $RGBbg["R"], $RGBbg["G"], $RGBbg["B"]); //Background
$fontkleur = imagecolorallocate($im, $RGBfont["R"], $RGBfont["G"], $RGBfont["B"]); //Font
} else {
//color pallet when errors occur (RED/WHITE)
$bg = imagecolorallocate($im, 255, 0, 0); //Red
$fontkleur = imagecolorallocate($im, 255, 255, 255); //White

//Write the text in the image
imagestring($im, $fontgroote, 3, 3, $tekst, $fontkleur);

//set content type to a PNG image
header("Content-type: image/png");

//Show the image

Now open up a new page and paste the code below inside it.

//address 1
echo '<img src="email.php?mg=jan&md=skynet&mext=be" alt="mail" />';
//address 2
echo '<img src="email.php?mg=johan&md=phpexamples&mext=net" alt="mail" />';
//address 3
echo '<img src="email.php?mg=ronny&md=test&mext=com" alt="mail" />';
Article was posted on Monday 25th of April 2011 @ 11:20 AM CEST   comment(s)