Convert Email Address To Image

As 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.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

16 Comments

  1. Thanks Alot,

    I have been looking for a way submit my email address to forums without getting spam!

    I really appreciate it

  2. Spike says:

    It would be better if instead using $_GET for the email, you use a simple variable because the way you use it it can be easily hacked :)

  3. John from Runlikeh3ll says:

    Wow, this is pretty cool. I usually just use email forms now, but I’ll keep this in mind. I remember making these in photoshop in the past to avoid spam bots :)

    • Scott Weaver says:

      John, yeah exactly. My boss ran this past me and I thought it was pretty useful since it’s pretty flexible in a web environment. Next, it needs different colors/fonts/sizes, but beyond that, not sure how else to improve it.

      Let me know if you use it. That’d be cool to see. :)

  4. Shaz from Photoshop Tutorials says:

    Its a great technique the amount of spam i get via spam bots that can actually damage or harm my computer is way out of order. Im going to be utilizing this technique thanks :)

  5. Shaz from Photoshop Tutorials says:

    thanks for sharing this, this is really helpful for emphasizing your e-mail address

  6. bethany from makeup artist in san antonio says:

    it is a great measure against spam bots to display your e-mail address as an image, and I definitely want to play around with different fonts to see how it looks :)

  7. Tomo from Free Business Cards says:

    this is a great way to pretty much hide your email to spambots. with the same purpose in mind, do an email form does the same?

  8. tekkie says:

    Very interesting idea indeed.

    Those having difficulties with programming (or have no access to PHP on their servers) would also find it handy to use a manual tool for obfuscation that works very much like the one bundled in Smarty template engine: obfuscatr. It’s a Mac OS X Dashboard widget that uses JavaScript to encode the email addy. Read more about it.

    • Scott Weaver says:

      That’s interesting that you mention the obfuscation method from Smarty, as I just started using it the other day. While I don’t think it’s as easy to use if you’re using something like, say, HTML to display your email it is definitely amazing at controlling what people see. Thanks for adding this point.

  9. Simon from Leadership says:

    Thanks very much for sharing this! I love how it basically shrinks the size of the image to as small as necessary to display the email properly – so excess space is taken up which could overwise make formatting tricky.

  10. Chris says:

    Nice script!! All I could improve was how it is called. I used.
    echo ‘ ‘; so no actual address goes on the page.
    I also added imagecolortransparent($im, $white);
    after line 37 to remove the white box around the text.

  11. Tes Pheakdey says:

    Nice code, short and useful. It is solve my problem on my website event I use ASP.NET on my website.

    Thank alot,

Leave a Reply

This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.