PHP Verification Code Implementation Local refresh

Source: Internet
Author: User
Tags rand

/*
* Auth_code () is a validation code function
* @access Public
* @param int $width Indicates the length of the verification code and defaults to 80
* @param int $height Indicates the height of the verification code, and defaults to 20
* @param int $num represents the number of digits in the verification code, which defaults to 4
* @param int $line represents the number of lines in the centerline of the validation code, which defaults to 4
* @param int $line represents the number of points in the verification code, default is 150
*/

The code is as follows Copy Code

function Auth_code ($width =, $height = $num = 4, $line = 4, $dot = 150) {

$length = Floor ($width/$num); Floor: Take integer part length: Average length per paragraph
for ($i =0; $i < $num; $i + +)
@ $rand. =dechex (Mt_rand (1,15));
$_session[' code ']= $rand;

Create a new artboard with a black bottom
$im =imagecreatetruecolor ($width, $height);

Take color
$green =imagecolorallocate ($im, 0,255,0);
$red = Imagecolorallocate ($im, 255,0,0);
$white =imagecolorallocate ($im, 255,255,255);
$black = Imagecolorallocate ($im, 0,0,0);

Fill artboard
Imagefill ($im, 0,0, $green);

Draw Line
for ($i =0; $i < $line; $i + +) {
$color =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imageline ($im, rand ($width -10), 0,rand (0, $width), $height, $color);
}
Draw a 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 a number
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 picture
Header ("Content-type:image/jpeg");
Imagejpeg ($im);
}

 -main functions:
       imagecreatetruecolor //new artboard with black bottom
        imagecolorallocate    //Fetch color
       imagefill                     //Fill artboard
       imageline                  //Draw line
        imagesetpixel          /DOT
        imagestring            /Draw String ( This code is numeric)
       Note: When you draw the digital verification code on the artboard, you should pay attention to the font out of bounds, so you should subtract some range, as detailed in the attachment.
   -Local Refresh verification code:

  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.