Php verification code encapsulation

Source: Internet
Author: User
Php verification code encapsulation /**
* Generate a verification code
* @ Param int $ type 1: only numbers, 2: only letters, 3: mixed numbers and letters
* @ Param int $ length
* @ Return string
*/
Function buildRandomString ($ type = 1, $ length = 4 ){
If ($ type = 1 ){
$ Chars = join ("", range (0, 9 ));
} Elseif ($ type = 2 ){
$ Chars = join ("", array_merge (range ("a", "z"), range ("A", "Z ")));
} Elseif ($ type = 3 ){
$ Chars = join ("", array_merge (range ("a", "z"), range ("A", "Z"), range (0, 9 )));
}
If ($ length> strlen ($ chars )){
Exit ("insufficient string length ");
}
$ Chars = str_shuffle ($ chars );
Return substr ($ chars, 0, $ length );
}

// Use the GD library for verification code

Function verifyImage ($ type = 1, $ length = 4, $ pixel = 0, $ line = 0, $ sess_name = "verify "){

Session_start ();
// Create a canvas
$ Width = 80;
$ Height = 28;
$ Image = imagecreatetruecolor ($ width, $ height );
$ White = imagcolorallocate ($ image, 255,255,255 );
$ Black = imagecolorallocate ($ image, 0, 0, 0 );
// Fill the canvas with a filled rectangle
Imagefilledrectangle ($ image, 1, 1, $ width-2, $ height-2, $ white );
$ Chars = buildRandomString ($ type, $ length );
$ _ SESSION [$ sess_name] = $ chars;
// $ Fontfiles = array ("MSYH. TTF "," MSYHBD. TTF "," SIMLI. TTF "," SIMSUN. TTC "," SIMYOU. TTF "," STZHONGS. TTF ");
$ Fontfiles = array ("SIMYOU. TTF ");
// Because the font file is large, only one font is retained. if necessary, you can add the font on your own. The fonts folder contains the fonts font, run the input fonts command to view the corresponding font.
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Size = mt_rand (14, 18 );
$ Angle = mt_rand (-15, 15 );
$ X = 5 + $ I * $ size;
$ Y = mt_rand (20, 26 );
$ Fontfile = $ fontfiles [mt_rand (0, count ($ fontfiles)-1)];
Putenv ('gdfontpath = '. realpath ('.'));
$ Color = imagecolorallocate ($ image, mt_rand (50, 90), mt_rand (80,200), mt_rand (90,180 ));
$ Text = substr ($ chars, $ I, 1 );
// Putenv ('gdfontpath = '. realpath ('.'));

// Name the font to be used (note the lack of the. ttf extension)
Imagettftext ($ image, $ size, $ angle, $ x, $ y, $ color, $ fontfile, $ text );
}
If ($ pixel ){
For ($ I = 0; $ I <50; $ I ++ ){
Imagesetpixel ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), $ black );
}
}
If ($ line ){
For ($ I = 1; $ I <$ line; $ I ++ ){
$ Color = imagecolorallocate ($ image, mt_rand (50, 90), mt_rand (80,200), mt_rand (90,180 ));
Imageline ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), mt_rand (0, $ width-1), mt_rand (0, $ height-1), $ color );
}
}

Header ("content-type: image/gif"); // sets the header information
Imagegif ($ image );
Imagedestroy ($ image );
}

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.