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: