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);
?>
http://www.example.com/image.php
image.php?email=test.user%40example.com
<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>