Php simple verification code

Source: Internet
Author: User
& Amp; lt ;! -- * @ Copyright (c) 2011 * @ author: Ernest * @ time: 2011 * @ version: new -- & amp; gt; & amp; lt ;? PhpclassVerifyImg {public $ fontSize15; defines the font size public $ length4; defines the string length...



Class VerifyImg {

Public $ fontSize = 15; // defines the font size.

Public $ length = 4; // define the string length

Public $ width = 70; // defines the image width.

Public $ height = 30; // define the image height

Public $ im = null; // generate an image with the specified width and height

Public $ font = 'C:/Windows/Fonts/Arial. ttf ';

Public $ strNum = "";

Public function Build (){

$ Strings = Array ('1', '2', '3', '4', '5', '6', '7', 'A ', 'B', 'C', 'D', 'e', 'F', 'H', 'I', 'J', 'K', 'M ', 'N', 'P', 'R','s ', 'T', 'u', 'V', 'W', 'X ', 'Y ');

// $ StrNum = "";

$ Count = count ($ strings );

For ($ I = 1; $ I <= $ this-> length; $ I ++) {// generate a string using random characters in a loop

$ StrNum. = $ strings [rand (0, $ count-1)];

}

Session_start ();

$ _ SESSION ["verifycode"] = $ strNum;

$ This-> im = imagecreate ($ this-> width, $ this-> height );

$ Backgroundcolor = imagecolorallocate ($ this-> im, 255,255,255); // generates the background color.

$ FrameColor = imageColorAllocate ($ this-> im, 0,255, 0 );

For ($ I = 0; $ I <$ this-> length; $ I ++ ){

$ CharY = ($ this-> height + 9)/2 + rand (-1, 1); // define the Y coordinate of the character

$ CharX = $ I * 15 + 8; // defines the X coordinate of the character

// $ Text_color = imagecolorallocate ($ this-> im, 255, 0, 0); // Generate character color

$ Text_color = imagecolorallocate ($ this-> im, mt_rand (50,255), mt_rand (50,128), mt_rand (50,200 ));

$ Angle = rand (-20, 20); // Generate the character angle

// Write characters

Imagettftext ($ this-> im, $ this-> fontSize, $ angle, $ charX, $ charY, $ text_color, $ this-> font, $ strNum [$ I]);

}

For ($ I = 0; $ I <= 5; $ I ++) {// draws the background line cyclically.

$ Linecolor = imagecolorallocate ($ this-> im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));

$ Linex = mt_rand (1, $ this-> width-1 );

$ Liney = mt_rand (1, $ this-> height-1 );

Imageline ($ this-> im, $ linex, $ liney, $ linex + mt_rand (0, 4)-2, $ liney + mt_rand (0, 4)-2, $ linecolor );

}

For ($ I = 0; $ I <= 32; $ I ++) {// draws the background point cyclically to generate the pitting effect.

$ Pointcolor = imagecolorallocate ($ this-> im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));

Imagesetpixel ($ this-> im, mt_rand (1, $ this-> width-1), mt_rand (1, $ this-> height-1), $ pointcolor );

}

Imagerectangle ($ this-> im, 0, 0, $ this-> width-1, $ this-> height-1, $ frameColor); // draw a border

Ob_clean ();

Header ('content-type: image/png ');

Imagepng ($ this-> im );

Imagedestroy ($ this-> im );

}

}

$ Img = new VerifyImg ();

$ Img-> Build ();

?>

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.