How PHP generates verification codes

Source: Internet
Author: User
Tags uppercase letter
The principle of generating verification code is very simple, a word ' painting '. Yes, the captcha we're going to draw with backgrounds, numbers or letters.

Effect:

The steps are as follows:
1. Get the Random verification code
With the GetCode function (custom), it returns a string.

2. Create an image resource, assign a color

$m = imagecreatetruecolor($width,$height);

Imagecolorallocate, this is actually getting a color.

3. Start painting
1). Fill the background color at the beginning of the upper left corner of the image

imagefill($m,0,0,$bg);

2). Add a colored rectangular box
Imagerectangle

3). Add interference points and interference lines

4). Take the verification code string image obtained

4. Output image
1). Direct output to Browser
Note that there is no output before this function executes, and no spaces
If the response header is not set, the page will appear garbled instead of an image of a captcha
Header ("Content-type:image/png"); Setting the response header information
Imagepng ($m);

2). Output to a file

imagepng($m,'test.png');

5. Destroying images
Imagedestroy ($m);

The code is as follows:

/** * @param int $num number of verification codes, default is 4 * @param int $type type of verification code, 0: pure number, 1: number + lowercase letter 2: number + uppercase letter * @param bool $outFile Verification code output To the file * @return Array returns the verification code and the verification code image name in an array */function Drawidentifycode ($num=4,$type=0,$outFile= True) {//Draw Verification Code$code= GetCode ($num,$type);//Get Verification code$width=$num*35;$height= +;//1. Creating a picture image resource, assigning colors$m= Imagecreatetruecolor ($width,$height);$c= Array (Imagecolorallocate ($m,Rand(0,255),Rand(0,255),Rand(0,255)), Imagecolorallocate ($m,Rand(0,255),Rand(0,255),Rand(0,255)), Imagecolorallocate ($m,Rand(0,255),Rand(0,255),Rand(0,255)), Imagecolorallocate ($m,Rand(0,255),Rand(0,255),Rand(0,255)));$BG= Imagecolorallocate ($m, -, -, -);//Background color//2. Start painting//Start area fill at the top left corner of imageImagefill ($m,0,0,$BG);//Add a colored rectangular boxImagerectangle ($m,0,0,$width-1, the,$c[0]);//Add interference points for($i=0;$i< -;$i+ +) Imagesetpixel ($m,Rand(0,$width),Rand(0, -),$c[$i%4]);//Add interference line for($i=0;$i<5;$i+ +) Imageline ($m,Rand(0,$width),Rand(0, -),Rand(0,$width),Rand(0, -),$c[$i%4]);//Draw Verification Code content (one character draw) for($i=0;$i<$num;$i+ +) Imagettftext ($m, -,Rand(- -, -), the+( -*$i), -,$c[$i%4],"Consola.ttf",$code[$i]);//3. Output Image$fileName= NULL;if(!$outFile)    {//Note that there is no output before this function is executed and no spaces//If the response header is not set, the page will appear garbled instead of an image of a captchaHeader"Content-type:image/png");//Set response header informationImagepng ($m); }Else{$fileName= Time ().'. png '; Imagepng ($m,$fileName); }//4. Destroying picturesImagedestroy ($m);returnArray$code,$fileName);}/** * @function randomly generate a verification code function * @param $m: Number of verification codes (default 4) * @param $type: type of captcha: 0: Pure number, 1: number + lowercase letter 2: number + uppercase Letter * @return return character Serial form of verification code * /function GetCode ($m=4,$type=0){$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";$t= Array (9, *, strlen ($str)-1);//Verification code type//Randomly generate verification code required content$c=""; for($i=0;$i<$m;$i++)$c.=$str[Rand(0,$t[$type])];return$c;}

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes how PHP generated verification code, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.