First, the principle of the Verification Code is to draw an image, write some words on the image, and then add some interference lines, pixels and other things, if you want to use php, you need to know which functions are drawn in php, and then use them. If you want to use the php drawing function, you must first enable the function of this module. Just remove the comments before php_gd2.dll in php. ini.
Draw a picture below:
The Code is as follows:
Session_start ();
// Generate a verification code Image
Header ("Content-type: image/PNG ");
$ Im = imagecreate (); // draw an image with the specified width and height
$ Back = ImageColorAllocate ($ im, 245,245,245); // defines the background color.
Imagefill ($ im, $ back); // fill the background color in the image you just drew
$ Vcodes = "";
Srand (double) microtime () * 1000000 );
// Generate four digits
For ($ I = 0; $ I <4; $ I ++ ){
$ Font = ImageColorAllocate ($ im, rand (100,255), rand (0,100), rand (100,255); // generate a random color
$ 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); // draws the pixel point function
}
ImagePNG ($ im );
ImageDestroy ($ im );
?>
This is basically how it works. In fact, if you add a watermark to an image, you can simply write it in the image.
Used directly
Enter the name of the PHP file.