This is a class which is different than every other class you can find regarding coverting an email address to image. This is because it uses the encryption of the email addresswhich is being passed to the script which renders the image. The renderer decrypts this parameter produces an image output.
Put both of these classes into root of your web: ClassEmailAsImage.php
<?php /** * ClassEmailAsImage - A class which inserts an email address as image instead of regular text to prevent spam * * @author Matej Koval http://www.codegravity.com * */ class ClassEmailAsImage { /** this is left blank, only if you want to use type the host name manually, then change "" to "http://yourhost.com/directory/" */ private static $hostUrl = ""; /** this is the key with which is the email address being encrypted, change it! */ private static $key = "this is my long key which I have to change first"; /** this is a path to the script which renders the image, you can change it too */ private static $script = "/emailAsImage.php"; /** paramter name which is being passed to the script producing image to decode the email address from */ private static $parameterName = "email"; /** * Helper function which encodes the special characters like /,+,= to be included into the URL * * @param encoded string with special characters $input * @return encoded string with special characters replaced */ function encodeSlashPlus($input) { $input = str_replace("/","_SLASH_", $input); $input = str_replace("+","_PLUS_", $input); $input = str_replace("=","_EQ_", $input); return $input; } /** * Helper function which decodes the special characters like /,+,= from URL * * @param encoded string with special characters replaced $input * @return encoded string with special characters */ function decodeSlashPlus($input) { $input = str_replace("_SLASH_", "/", $input); $input = str_replace("_PLUS_", "+", $input); $input = str_replace("_EQ_", "=", $input); return $input; } /** * Basic function which performs the encryption * * @param value to be encrypted $value * @return encryption result with special characters replaced */ function encrypt($value){ $text = $value; $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $crypttext = mcrypt_encrypt(MCRYPT_3DES, ClassEmailAsImage :: $key, $text, MCRYPT_MODE_ECB, $iv); return ClassEmailAsImage :: encodeSlashPlus(base64_encode($crypttext)); } /** * Basic function which performs the decryption * * @param value to be decrypted, with special characters replaced $value * @return decryption result which returns the original value */ function decrypt($value){ $crypttext = base64_decode(ClassEmailAsImage::decodeSlashPlus($value)); $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $decrypttext = mcrypt_decrypt(MCRYPT_3DES, ClassEmailAsImage::$key, $crypttext, MCRYPT_MODE_ECB, $iv); return trim($decrypttext); } /** * Function which uses the PHP GD library to render the image * which was taken from the parameter and decrypted to get the original value */ function renderImage() { $parameterName = ClassEmailAsImage::$parameterName; $myText = ClassEmailAsImage::decrypt($_GET[$parameterName]); $myTextLen = (strlen($myText) * 10); $safeemail = imagecreate($myTextLen,20); $backcolor = imagecolorallocate($safeemail,255,255,255); $textcolor = imagecolorallocate($safeemail,0,0,0); imagefill($safeemail,0,0,$backcolor); Imagestring($safeemail,2,5,5,$myText,$textcolor); header("Content-type: image/jpeg"); imagejpeg($safeemail); } /** * Function called from the emailAsImage.php, which produces the URL to the image, * and appends a parameter with already encrypted email * * @param email to be encrypted $email * @return img HTML tag which points to the emailAsImage.php with encrypted email as parameter */ function render($email) { if (ClassEmailAsImage::$hostUrl) { $hostUrl = ClassEmailAsImage::$hostUrl; } else { $self = rtrim( dirname( $_SERVER['PHP_SELF'] ), '/\\' ) ; $hostUrl = $_SERVER['HTTP_HOST'] . $self; } $value = ClassEmailAsImage::encrypt($email); $url = "http://".$hostUrl. ClassEmailAsImage::$script . "?".ClassEmailAsImage::$parameterName."=".$value; $output = "<img src='".$url."' height='20' border='0'/>"; return $output; } } ?>
emailAsImage.php <?php require("ClassEmailAsImage.php"); ClassEmailAsImage::renderImage(); die(); ?> Produced HTML output:
Result on page:  Usage in your scriptsThis is very simple: require("ClassEmailAsImage.php"); ClassEmailAsImage::render($myEmailVariable);
You can get the complete source code in the Download section.
|
Comments
RSS feed for comments to this post