开发者

PHP create image to display email address?

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

Meaning, if I want to display "joe@example.com" on my web page, since crawlers can easily find that text - I want to display the email address as an image that开发者_如何学Python says "joe@example.com".

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

  • color: #20c
  • font: 11px normal tahoma


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


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


You should use gd image processing library.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜