Encoding Your E-mail Online As An Image

Posting your e-mail in plain text format on a web page is a guaranteed way of being signed up to a spam mailing list. When I did a Google search for “email encoder” I was disappointed to find a bunch of websites which encoded the e-mail through a JavaScript function. This method offers some protection, but is not a good method to protect you from the smarter bots that crawl the internet snapping up e-mail addresses. All it takes is a bot which can convert the HTML character entities back into its original ASCII codes to snatch your email.

In my opinion, the best way to protect your e-mail address is to encode it in an image. This is still not 100% fool proof, but the majority of bots cannot crawl and look at the text contents of an image. I couldn’t find a simple script available online that would encode an email address as an image, so I wrote a PHP script that would do the trick.

/**
 * @file email.php
 * This script generates the e-mail address text and acts as a PNG image.
 */
header( "Content-type: image/png" );

define("SITE_EMAIL", "email@example.com");
define("EMAIL_FONT_SIZE", 4);

$intEmailLength = strlen( SITE_EMAIL );
$objImage = imagecreate( imagefontwidth( EMAIL_FONT_SIZE ) * $intEmailLength, imagefontheight( EMAIL_FONT_SIZE ) );
$intBackgroundColor = imagecolorallocate( $objImage, 255, 255, 255 );
$intTextColor = imagecolorallocate ( $objImage, 0, 0, 0 );
imagecolortransparent( $objImage, $intBackgroundColor );
imagestring ( $objImage, EMAIL_FONT_SIZE, 0, 0,  SITE_EMAIL, $intTextColor );
imagepng( $objImage );

Of course, define SITE_EMAIL with your e-mail address and EMAIL_FONT_SIZE with the size of the font that you’d like (it was 4 in my case).

This PHP script will generate the image, so all you need to do is link to it in the following way to display it on your web page:

<img src="email.php" />

Leave a Reply

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