views:

55

answers:

4

Using PHP: How can I create an image to display an email address (to help reduce spam)?

Meaning, if I want to display "[email protected]" on my web page, since crawlers can easily find that text - I want to display the email address as an image that says "[email protected]".

How do I do that? I want the font to be:

  • color: #20c
  • font: 11px normal tahoma
+1  A: 

A simple search that you could easily do....

However: (color, font and string not the ones you specified)

header("Content-type: image/png");
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);

Relevant function definitions:

php.net/imagecreate

php.net/imagestring

acmatos
"However" what?
Teddkl
However, here is the code to get you started, as is @ php.net/imagecreate...
acmatos
A: 

You should use gd image processing library.

sundowatch
+2  A: 

Use these:

  • header, to tell the browser to expect an image instead of HTML (PHP's default). The image function doc pages have more information about this.
  • imagettfbbox, to find out the required size for the image
  • imagecreatetruecolor, to create the image resource
  • imagecolorallocate, to allocate a color for the text
  • imagettftext, to draw the text (read the example, it's almost all you need)
  • imagepng, to output the image to the browser
Matti Virkkunen
+1 But please make it a list with some additional information why/how to use these functions.
Gumbo
The documentation links should be enough for anyone who can be bothered to spend five minutes reading.
Matti Virkkunen
Oh, and you forgot `header`.
Gumbo
Thought it would be obvious since each and every one of those documentation pages has it, but here you go...
Matti Virkkunen
A: 

If you are only doing this for one address you should not be doing this dynamically everytime the page loads for performance reasons. If this is the case you can find amny email obfuscator online such as this one:

http://digitalcolony.com/lab/maskemail/maskemail.aspx

Steve Robillard
No, I'm doing this for hundreds of email addresses
Teddkl