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="" />