Home > Articles, Internet Applications, Security Systems & Access Control > Post your email address to the public as an image to avoid spam.

Post your email address to the public as an image to avoid spam.

Whenever you want to post your email address to the public and don’t want to use a Contact Form which usually hides the address, try to display the address of the email as an image in order to avoid spam.

One way to achieve this is to manually create your email address as an image and embed it to your contact page. Most of the times we want this operation to be automatically. So, I have created a simple example to show you how you can make this possible.

There is a PHP file called “generate-email-image.php” which generates a PNG image with your email address :

<?php
  // generate email address as an image to avoid spam.

  /*
   *  Copyright (C) 2011  Efstathios Chatzikyriakidis <contact@efxa.org>
   *
   *  This program is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program. If not, see <http://www.gnu.org/licenses/>.
   */

  // include constants.
  require_once ("constants.php");

  // try to transform a text message to an image.
  function text2image ($text, $font = 3, $red = 0, $green = 0, $blue = 0) {
    // calculate the image size (width, height).
    $width  = 2 + utf8_strlen ($text) * imagefontwidth ($font);
    $height = 2 + imagefontheight ($font);

    // create the image with the specified size.
    $im = imagecreate ($width, $height);

    // set up the background and text color.
    $white = imagecolorallocate ($im, 255, 255, 255);
    $color = imagecolorallocate ($im, $red, $green, $blue);

    // adjust the image to be transparent.
    imagecolortransparent ($im, $white);

    // write the text to the image.
    imagestring ($im, $font, 1, 1, $text, $color);

    // return the image.
    return $im;
  }

  // send to the browser the type of the following data.
  header ("Content-type: image/png");

  // send to the browser the email address as an image.
  imagepng (text2image (CONFIGURATION_EMAIL_ADDRESS));
?>

In order to display it, you can call it in an “img” HTML tag in any web page :

<img src="generate-email-image.php" alt="" />
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: