Home > Article > Backend Development > How to implement the function of converting text into pictures using PHP
This article mainly introduces the principle and implementation method of PHP text to image function, and analyzes the relevant configuration and usage of PHP based on the gd2 extension library to generate images based on specific examples. Friends in need can refer to the following
The example in this article describes the PHP text to image function. Share it with everyone for your reference, the details are as follows:
This function is mainly used to process important information such as email addresses, mobile phones, etc. that may be crawled by web crawlers. It's definitely a good idea to convert text into images. The basic generation principle of the verification code is similar to this, but the generation process of converting text into images is further complicated, making it impossible for scanning machines to recognize it. Converting text to images in php is very simple. First open php.ini in the php installation directory, find extension=php_gd2.dll, remove the quotation marks in front of it, open php's gd2 extension library, and you can use it directly. PHP keyword to convert text into images.
The directory structure is as follows. img_generator.php is the php used to convert text to images, and index.html is the page presented to the user.
#index.html code is very, very simple. Create an img tag, write img_generator.php in the src, and then send it to img_generator.php with a text parameter. A Get request, the img_generator.php page is essentially a picture generated based on parameters.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>图片测试</title> </head> <body> <img src="img_generator.php?text=abc@test.com"> </body> </html>
img_generator.php code is as follows, through $_REQUEST["text"];
get the text description passed by the img tag in index.html, and then use a series of php Keywords, that is, pictures generated by inherent methods.
<?php $text=$_REQUEST["text"];//显示的文字 $size=12;//字体大小 $font="c:/windows/fonts/SIMHEI.TTF";//字体类型,这里为黑体,具体请在windows/fonts文件夹中,找相应的font文件 $img=imagecreate(500,24);//创建一个长为500高为16的空白图片 imagecolorallocate($img,0xff,0xff,0xff);//设置图片背景颜色,这里背景颜色为#ffffff,也就是白色 $black=imagecolorallocate($img,0x00,0x00,0x00);//设置字体颜色,这里为#000000,也就是黑色 imagettftext($img,$size,0,0,16,$black,$font,$text);//将ttf文字写到图片中 header('Content-Type: image/png');//发送头信息 imagepng($img);//输出图片,输出png使用imagepng方法,输出gif使用imagegif方法 ?>
In essence, the principle is the same as in "php voting system: adding and deleting votes (administrator)".
The above is the detailed content of How to implement the function of converting text into pictures using PHP. For more information, please follow other related articles on the PHP Chinese website!