A handy PHP verification code instance sharing _ php instance

Source: Internet
Author: User
This article mainly introduces a useful PHP verification code instance. if you need it, you can refer to it to share a useful php verification code class, including call examples.
Note:
If the specified font is not applicable, use the imagestring () function. if you need the specified font, use the imagettftext () function. The font is on drive C for Windows/Fonts.

Refer to the php verification code generation method on the Internet, as well as the php image verification code and php Chinese verification code generation method. Use related knowledge of php gd Library.

1. VerificationCode. class. php

The code is as follows:


Class VerificationCode {
Private $ charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; // random factor
Private $ code; // verification code
Private $ codelen = 4; // verification code length
Private $ width = 110; // width
Private $ height = 30; // height
Private $ img; // image resource handle
Private $ font; // specifies the font.
Private $ fontSize = 25; // font size
Private $ fontColor; // font color
Public function _ construct (){
$ This-> font = "CALIBRIZ. TTF ";
}
// Generate the verification code
Private function createCode (){
$ Len = strlen ($ this-> charset)-1;
For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
$ This-> code. = $ this-> charset [mt_rand (0, $ len)];
}
}
// Generate background
Private function createBg (){
$ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
$ Color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255 ));
Imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color );
}
// Generate text
Private function createFont (){
$ X = $ this-> width/$ this-> codelen;
For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
$ This-> fontColor = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
Imagettftext ($ this-> img, $ this-> fontSize, mt_rand (-30, 30), $ I * $ x + mt_rand (1.4), $ this-> height, $ this-> fontColor, $ this-> font, $ this-> code [$ I]); // www.jb51.net
// Imagestring ($ this-> img, 5, $ I * $ x + mt_rand (1, 5), 5, $ this-> code [$ I], $ this-> fontColor );
}
}
// Generate lines and snowflakes
Private function createDisturb (){
For ($ I = 0; $ I <6; $ I ++ ){
$ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
Imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> width), mt_rand (0, $ this-> width), mt_rand (0, $ this-> width), $ color );
}
For ($ I = 0; I I <100; $ I ++ ){
$ Color = imagecolorallocate ($ this-> img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ),'*', $ color );
}
}
// Output
Private function outPut (){
Header ("Content-Type: image/png ");
Imagepng ($ this-> img );
Imagedestroy ($ this-> img );
}
Public function showCode (){
$ This-> createBg ();
$ This-> createCode ();
$ This-> createDisturb ();
$ This-> createFont ();
$ This-> outPut ();
}
// Obtain the verification code
Public function getCode (){
Return strtolower ($ this-> code );
}
}
?>

Code. php

The code is as follows:


Session_start ();
Require_once 'verificationcode. class. php ';
$ Code = new VerificationCode ();
$ _ SESSION ['code'] = $ code-> getCode ();
$ Code-> showCode ();
?>

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