MattsBits
MattsBits

MattsBits

Convert Email Address To Image Using PHP To Avoid Harvesting  

by Matt Hawkins, 04/04/2011
Categories : PHP & MySQL

When you display an email address on a website as text there is a risk that it will be harvested and used by spammers. An easy way to avoid this is to display the email as an image. That way when the spammer harvests the text from your page they will not find the email address and you will have some protection. It is not perfect but better than nothing.

The following article shows how you generate an image dynamically using PHP and the GD image processing library. It assumes the GD library is installed on your PHP system or web host.

Step 1 - Create image.php
Create a text file called image.php and paste in the following php code :

<?php
// Set header to png type
header("Content-type: image/png");

// Get email address to use from URL
if(isset($_GET['email'])) {
  $email = $_GET['email'];
} else {
  $email = "user@example.com";  
}

// Get font size to use from URL
if(isset($_GET['font'])) {
  $font = $_GET['font'];
} else {
  $font = 4;  
}

// Get size of font characters
$fontwidth  = imagefontwidth($font);
$fontheight = imagefontheight($font);

// Define total size of border around the text
$border_x = $fontwidth*3;
$border_y = $fontheight;

// Determine size of image
$length =    (strlen($email)*$fontwidth) + $border_x;
$height =    $fontheight + $border_y;

// Create image using GD image library
$im = @ImageCreate ($length, $height)
    or die ("Cannot Initialize new GD image stream");

// Set background colour to yellow
$background_color = ImageColorAllocate ($im, 255, 255, 0);

// Set text colour to black
$text_color = ImageColorAllocate ($im, 0, 0, 0);

// Write text onto image
imagestring($im, $font,($border_x/2),($border_y/2),$email, $text_color);

// Generate and output PNG image
imagepng ($im);
?>


This file will generate an image containing an email address. If no email address is specified it defaults to "user@example.com".

Step 2 - Test

You can now call the page directly and it should display an image containing the email address "user@example.com".

http://www.example.com/image.php


If that works ok try the following :

image.php?email=test.user%40example.com


This specifies the email address to use. The specified email address must be URL encoded which in most cases means presenting the "@" symbol as "%40".

Step 3 - Implement In Your HTML

Within your website you can use this example HTML to display the email image as required :

<p>image.php :<br><img src="image.php" alt="Email Address" /></p>
<p>image.php?email=test.user%40example.com:<br>
<img src="image.php?email=test.user%40example.com" alt="Email Address" /></p>
<p>image.php?email=test.user%40example.com&font=0<br>
<img src="image.php?email=test.user%40example.com&font=0" alt="Email Address" /></p>
<p>image.php?email=test.user%40example.com&font=5<br>
<img src="image.php?email=test.user%40example.com&font=5" alt="Email Address" /></p>


The size parameter can be used to change the size of the font and accepts values in the range 0-5.


The above example assumes the image.php is in the same directory as your html page.

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2017