Home  >  Article  >  Backend Development  >  PHP verification code example code_PHP tutorial

PHP verification code example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:48709browse

If you want to use PHP's drawing function, you must first enable the function of this module. Just remove the comment in front of php_gd2.dll in php.ini.
Start drawing below:

Copy code The code is as follows:

session_start();
//Generate verification code image
Header("Content-type: image/PNG");
$im = imagecreate(44,18); // Draw a picture with specified width and height
$back = ImageColorAllocate($im, 245,245,245); //Define the background color
imagefill($im,0,0,$back); //Fill the background color into the image just drawn
$ vcodes = "";
srand((double)microtime()*1000000);
//Generate 4-digit number
for($i=0;$i<4;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // Generate random colors
$authnum=rand(1,9);
$vcodes.= $authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}
$_SESSION['VCODE'] = $vcodes;
for($i=0;$i<100;$i++) //Add interference pixels
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255) );
imagesetpixel($im, rand()%70, rand()%30, $randcolor); // Draw pixel function
}
ImagePNG($im);
ImageDestroy( $im);
?>

This is basically how it is implemented. In fact, if you watermark a picture, it is nothing more than writing into the picture. The principles are similar.
Where to use it directly
Fill in the name of this php file and you can use it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322029.htmlTechArticleIf you want to use PHP's drawing function, you must first enable the function of this module. Just remove the comment in front of php_gd2.dll in php.ini. Start drawing below: Copy the code The code is as follows...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn