PhpRiot
Download Article
Download this article or the entire “Anti-Spam Techniques In PHP” series with all listings and files.




More information
Related Articles
Related Books
Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)

Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)

If you know HTML, this guide will have you building interactive websites quickly. You'll learn...

Smarty PHP Template Programming And Applications

Smarty PHP Template Programming And Applications

A step-by-step guide to building PHP web sites and applications using the Smarty templating...
PhpRiot Newsletter
Your Email Address:

More information

Anti-Spam Techniques In PHP, Part 1

Technique 3: Images

This technique is the most difficult for email harvesters to deal with. It involves never having your email address appear in text (be it encoded or in plaintext), but rather displaying an image which contains your email address. If you put it in the same font and size as the rest of your site (keeping it anti-aliased), most people will never realise. Although as with technique 2, you can’t have it hyperlinked directly.

There are two ways to implement this.

The first way is to use something like Photoshop to create the image with your email address in. This takes only a minute or two and you’re done. The problem with this is that if your email address ever changes you need to recreate the image. Also, this has to be done for every email address you want to publish, so you can’t do any dynamic image creation based on say, email address of people who submit content.

The second way is to use something like the PHP image functions to create email images. Just create a script which draws the text on a small canvas (there a millions of tutorials on the web to do this), and display it.

Listing 7 listing-7.html
My email address is <img src="/images/emailAddress.php" alt="" class="email-image" />

Here’s an example of what the emailAddress.php script will look like:

Listing 8 emailAddress.php
<?php
    // the email address to draw
    $email = 'antispam@example.com';
 
    // select the font. '4' is a builtin kind,
    // with each letter about 8px wide
    $font = 4;
    $width = strlen($email) * 8; // 8px wide per letter
    $height = 15; // this font size needs about this height
 
    // create the GD image
    $im = ImageCreate($width, $height);
 
    // allocate the background colour. The first call
    // to this function sets the background
    $white = ImageColorAllocate($im, 255, 255, 255);
 
    // the text colour.. black
    $textColor = ImageColorAllocate($im, 0, 0, 0);
 
    // write the email address to the image
    ImageString($im, $font, 0, 0, $email, $textColor);
 
    // output the content-type header and the image
    header('Content-type: image/jpeg');
    imageJpeg($im);
?>

You can also use ImageTTF() to draw the text with truetype fonts, and then use the ImageTTFBBox() function to know how big to create the image, but this code is at least a starting point.

If this image doesn’t line up quite right, perhaps use this little bit of CSS:

Listing 9 listing-9.css
.email-image { vertical-align : middle; }

Alternatively, let’s say Joe submitted an article to a web site, and the article has an ID of 132. If you wanted to display Joe’s email address when viewing his article, you could do:

Listing 10 listing-10.html
Contact Joe at <img src="/images/emailAddress.php?id=132" alt="" class="email-image" />

Then in your emailAddress.php script, it looks up the email address to generate based on the $_GET['id'] (which in this case is 132).

In This Article