Example of making PHP image verification code

Source: Internet
Author: User
Tags imagejpeg random seed
    1. $authnum = ";
    2. $ychar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
    3. $list =explode (",", $ychar);//Split function
    4. for ($i =0; $i <4; $i + +) {
    5. $randnum =rand (0,35);
    6. $authnum. = $list [$randnum];//output as an array
Copy Code

Method Two, defined as a private function.

    1. Private Function Createcheckcode ()
    2. {
    3. For (i=0;i codenum;i++)
    4. {
    5. Number = rand (0,2);
    6. Switch (number)
    7. {
    8. Case 0:rand_number = rand (48,57); break;//Digital
    9. Case 1:rand_number = rand (65,90); break;//Capital Letter
    10. Case 2:rand_number = rand (97,122); break;//lowercase Letter
    11. }
    12. $ASC = sprintf ("%c", Rand_number);
    13. $ASC _number = ASC_NUMBER.ASC;
    14. }
    15. return asc_number;
    16. }
Copy Code

Method Three, use random seed to generate PHP verification code.

    1. Srand (Microtime () *100000);//equivalent to a timer
    2. $string = "abcdefghigklmnopqrstuvwxyz123456789";
    3. for ($i =0; $i <4; $i + +)
    4. {
    5. $new _number.= $string [Rand (0,strlen ($string)-1)];//immediately produces an array
    6. }
Copy Code

Method Four,

    1. for ($i =0; $i <4; $i + +)
    2. {
    3. $rand. =dechex (rand (1,15));//convert decimal to hexadecimal
    4. }
Copy Code

Next enter the focus of this article: PHP GD Library: (provides a series of image processing functions of IPI, generated image processing image) enable PHP in the GD library: php.ini configuration file, remove the "; Extension=php_gd2.dll" in ";"; Part of the GD library functions: 1.imagecreatetruecolor (int x_size,int y_size) New true Color Image 2.imagecolorallocate (Resource Image,int Red,int Green,int blue) assigns a color to an image, the primary 3.imagestring (Resource,font,int x,int y,content,color) Drawing function 4.header ("Content-type: Image/jpeg ") output function the header of PHP is defined as the action of the head, the type in the PHP5 support 3:1,content-type:xxxx/yyyy 2,location:xxxx:yyyy/zzzz 3,status:nnn XXXXXX xxxx/yyyy represents the type of content file such as: Image/gif image/jpeg image/png imagejpeg (), Imagegif (), Imagepang () 5.iamgeline (Resource Image,int x1,int y1,int x2,int y2,int color); Draw line function, (int x,int y) Start coordinate 6.imagesetpixel (resource image,int x,int y,int color) Draw point function 7.imagettftext (Resource image,float Size,float angle,int x,int y,int color,string fontfile,string text) with font write function 8.iconv ("gb2312", "Utf-8", "string");// First of all to convert text into utf-8 format PHP code to insert Chinese method, here can refer to the next PHP Chinese code generation method.

Example 1, randomly generated numbers, letters of code:

    1. che.php
    2. Session_Start ();
    3. for ($i =0; $i <4; $i + +)
    4. {
    5. $rand. =dechex (rand (1,15));
    6. }
    7. $_session[' Check_num ']= $rand;
    8. $image =imagecreatetruecolor (50,30);
    9. $BG =imagecolorallocate ($im, 0,0,0);//When using the palette for the first time, the background color
    10. $te =imagecolorallocate ($im, 255,255,255);
    11. Imagestring ($image, 6,rand (0,20), Rand (0,2), $rand, $te);
    12. Ob_clean (); the image "Http://localhost/**.php" appears in the//php Web page because it has to generate a verification code and cannot be displayed because of its own error
    13. Header ("Content-type:image/jpeg"); Imagejpeg ($image);
    14. ?>
Copy Code

Example 2, draw a line of interference code for the picture:

    1. for ($i =0; $i <8; $i + +)//Draw more than one line
    2. {
    3. $CG =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//produce random colors
    4. Imageline ($im, Rand (10,40), 0,rand (10,40), $CG);
    5. }
Copy Code

Example 3, the code for the image to draw a disturbance point:

    1. for ($i =0; $i <80; $i + +)//Draw multiple points
    2. {
    3. Imagesetpixel ($im, Rand (0,40), Rand (0,20), $CG);
    4. }
Copy Code

Example 4, write the text to the image code:

    1. $str =array (' i ', ' I ', ' pro ', ' Pro ');//store displayed Kanji

    2. for ($i =0; $i <4; $i + +)
    3. {
    4. $sss. = $str [Rand (0,3)];//random display of Chinese characters
    5. }

    6. $str =iconv ("gb2312", "Utf-8", $str); Chinese character coding transformation, I don't seem to need

    7. Imagettftext ($im, 10,0,rand (5,60), Rand (5,60), $te, "Simhei.ttf", $sss);//

Copy Code

0: The tilt of the font, "Simhei.ttf": Font style, usually placed in the root directory;

This completes the PHP picture verification Code of the entire production process, script small hope this article to use the GD library to generate PHP verification code, have some help.

  • 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.