PHP create image to display email address?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 10:13:49

问题


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 says "joe@example.com".

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

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

回答1:


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




回答2:


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



回答3:


You should use gd image processing library.




回答4:


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



来源:https://stackoverflow.com/questions/2836019/php-create-image-to-display-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!