Php verification code for local refreshing

Source: Internet
Author: User

We usually need to implement partial refresh when using the verification code. This is inevitable and also a point to improve the user experience. Let's look at the usage of a php verification code in practical applications, for more information, see.

/*
* Auth_code () is the verification code function.
* @ Access public
* @ Param int $ width indicates the length of the verification code. The default value is 80.
* @ Param int $ height indicates the height of the verification code. The default value is 20.
* @ Param int $ num indicates the number of digits in the verification code. The default value is 4.
* @ Param int $ line indicates the number of lines in the middle line of the verification code. The default value is 4.
* @ Param int $ line indicates the number of vertices in the verification code. The default value is 150.
*/

The Code is as follows: Copy code

Function Auth_code ($ width = 80, $ height = 20, $ num = 4, $ line = 4, $ dot = 150 ){

$ Length = floor ($ width/$ num); // floor: integer part length: average length of each segment
For ($ I = 0; $ I <$ num; $ I ++)
@ $ Rand. = dechex (mt_rand (1, 15 ));
$ _ SESSION ['code'] = $ rand;
 
// Create a canvas with a black background
$ Im = imagecreatetruecolor ($ width, $ height );
 
// Color
$ Green = imagecolorallocate ($ im, 0,255, 0 );
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Black = imagecolorallocate ($ im, 0, 0 );
 
// Fill the canvas
// Imagefill ($ im, 0, 0, $ green );

// Draw a line
For ($ I = 0; $ I <$ line; $ I ++ ){
$ Color = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imageline ($ im, rand (10, $ width-10), 0, rand (0, $ width), $ height, $ color );
}
// Draw point
For ($ I = 0; $ I <$ dot; $ I ++ ){
$ Color = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand (0, $ width), rand (0, $ height), $ color );
}
// Draw numbers
For ($ I = 0; $ I <$ num; $ I ++ ){
Imagestring ($ im, 5, mt_rand ($ I * $ length + 1, ($ I + 1) * $ length-8), mt_rand (0, $ height-14 ), $ _ SESSION ['code'] [$ I], $ white );
}
 
// Output image
Header ("content-type: image/jpeg ");
Imagejpeg ($ im );
}

-Main functions:
Imagecreatetruecolor // create a canvas with a black background
Imagecolorallocate // color
Imagefill // fill the canvas
Imageline // draw a line
Imagesetpixel // image point
Imagestring // draw a string (this verification code is a number)
Note: When drawing a digital verification code on the canvas, you must note that the font is out of bounds. Therefore, you must subtract some ranges. For details, see the attachment.
-Partial refresh verification code:

The Code is as follows: Copy code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.