Convert Email Address To Image
Posted in PHP, Web Dev on February 27th, 2009 by Scott Weaver – 15 CommentsAs a measure against spam bots and things of that nature, I’ve seen a site or two displaying e-mail addresses as images.
So to make things extremely simple, I made a very quick script to do this dynamically-
/***********************************************
* Email Address Image
* by Scott Weaver
*
* Last updated: February 27th, 2009 @ 2:30pm
*
* Converts an e-mail address to an image to
* prevent spam bots and scrapers from stealing
* it.
*
***********************************************/
/*
Requires two inputs:
u : is the username part of the email address
d : is the domain part of the email address
*/
if( !isset($_GET['u']) || !isset($_GET['d']) )
die('Please enter a valid e-mail address.');
$email = $_GET['u'] . '@' . $_GET['d'];
/*
For this font, the width is 6X the character length,
plus 1 for padding.
*/
$width = (strlen( $email )*6)+1;
// Create image w/ color
$im = imagecreate($width, 14)
or die('Cannot initialize new GD image stream');
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// Write email address to image
imagestring($im, 2, 1, 0, $email, $black);
// Output & destroy
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Of course, you’ll notice that anyone who mods a script to look for email addresses matching the script’s parameters would be able to circumvent this little trick, but overall it should work.
What’s cool is that the script will auto-adjust the image for the width of the e-mail and it doesn’t take up much space by itself (just enough to display uppercase and lowercase letters).
Instructions
Just name the file something like ‘emailimg.php’ and upload it to your server. Once uploaded, you can reference it just like so:
<img src=”emailimg.php?u=johntheripper&d=hacker.net” />
That’s it. You’re done.
Demo
Here’s a working example:
Requirements
The only required functions are: imagecreate(), imagecolorallocate(), imagestring(), imagepng() and imagedestroy(). Basically, you just need the GD library enabled for PHP.
Improvements
I think the only improvements you could add to this script would be the ability to use different fonts, and to change the background/foreground colors. Well, that and a way to better hide the email address parameters. Maybe you can think of some that I can’t.








