PHP Verification Code class ValidateCode resolution, verification code validatecode
PHP parsing Verification Code
1. Start
I can see the ValidateCode generation Verification Code Class written in PHP on the Internet. It feels good. Let's take a look at it.
2. Class Diagram
3. Some verification code
3.1 define variables
// Random factor private $ charset = 'abcdefghjkmnprstuvwxyzabcdefgjkmnprstuvwxyz23456789 '; private $ code; private $ codeLen = 4; private $ width = 130; private $ helap = 50; private $ img; // image private $ font; // font private $ fontsize = 20;
$ Charset is a random factor, which removes a few uneasily distinguished characters, such as letters "I, l, o, q", numbers ". It is necessary to add some Chinese or other characters or expressions.
$ CodeLen indicates the length of the verification code, which has four common characters.
3.2 constructor, set the verification code font to generate a true color image img
public function __construct() { $this->font = ROOT_PATH.'/font/Chowderhead.ttf'; $this->img = imagecreatetruecolor($this->width, $this->heigh); }
3.3 4 characters are randomly selected from the random factor as the $ code verification code.
// Generate random code private function createCode () {$ _ len = strlen ($ this-> charset)-1; for ($ I = 0; $ I <$ this-> codeLen; $ I ++) {$ this-> code. = $ this-> charset [mt_rand (0, $ _ len)] ;}}
3.4 generate the verification code background color.
// Generate the background private function createBg () {$ color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255 )); imagefilledrectangle ($ this-> img, 0, $ this-> heigh, $ this-> width, 0, $ color );}
Mt_rand (157,255) is used to randomly obtain lighter colors.
3.5 generate text on the image.
// Generate the text private function createFont () {$ _ x = $ this-> width/$ this-> codeLen; $ _ y = $ this-> heweigh/2; for ($ I = 0; $ I <$ this-> codeLen; $ I ++) {$ color = imagecolorallocate ($ this-> img, mt_rand (0,156 ), mt_rand (0,156), mt_rand (0,156); imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30, 30 ), $ _ x * $ I + mt_rand (3, 5), $ _ y + mt_rand (2, 4), $ color, $ this-> font, $ this-> code [$ I]);}
The text of the verification code is generated on the image, mainly considering the position of the text on the image and the color of each text.
Control the x-axis position of the nth text = (image width/verification code length) * (n-1) + random offset number, where n = {d1.... n}
Control the y-axis position of the nth text = Image Height/2 + random offsets;
Mt_rand (0,156) random text color, 0-is to take a deeper color.
Mt_rand (-30, 30) random text rotation.
3.6 generate lines and snowflakes on the Image
// Generate a line. Snowflake private function createLine () {for ($ I = 0; $ I <15; $ 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-> heweigh), mt_rand (0, $ this-> width), mt_rand (0, $ this-> heweigh ), $ color) ;}for ($ I = 0; $ I <150; $ 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-> heigh), '#', $ color );}}
When drawing a line, take a deeper color value, while when painting a snowflake, take a lighter color value, as far as possible, do not affect the human eye recognition verification code, it can also interfere with the automatic verification code identification mechanism.
3.7 generate Verification Code images for external calls.
// Generate public function doImg () {$ this-> createBg (); // 1. create Verification Code background $ this-> createCode (); // 2. generate random code $ this-> createLine (); // 3. generate lines and snowflakes $ this-> createFont (); // 4. generate text $ this-> outPut (); // 5. output Verification Code image}
3.8 complete code:
<? Php/*** Created by PhpStorm. * User: andy * Date: 16-12-22 * Time: */class ValidateCode {// random factor private $ charset = 'abcdefghjkmnprstuvwxyzabcdefgjkmnprstuvwxyz23456789 '; private $ code; private $ codeLen = 4; private $ width = 130; private $ heigh = 50; private $ img; // image private $ font; // font private $ fontsize = 20; public function _ construct () {$ this-> font = ROOT_PATH. '/font/Chowderhead. ttf'; $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> heigh);} // generate random 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 the background private function createBg () {$ color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255); imagefilledrectangle ($ this-> img, 0, $ this-> heweigh, $ this-> width, 0, $ color);} // generate the text private function createFont () {$ _ x = $ this-> width/$ this-> codeLen; $ _ y = $ this-> heigh/2; for ($ I = 0; $ I <$ this-> codeLen; $ I ++) {$ color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30, 30), $ _ x * $ I + mt_rand (3, 5), $ _ y + mt_rand (2, 4 ), $ color, $ this-> font, $ this-> code [$ I]) ;}// generate a line, snowflake private function createLine () {for ($ I = 0; $ I <15; $ 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-> heweigh), mt_rand (0, $ this-> width), mt_rand (0, $ this-> heigh), $ color);} for ($ I = 0; $ I <150; $ 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-> heigh), '#', $ color );}} // outPut image private function outPut () {header ('content-Type: image/png '); imagepng ($ this-> img ); imagedestroy ($ this-> img);} // public function doImg () {$ this-> createBg (); // 1. create Verification Code background $ this-> createCode (); // 2. generate random code $ this-> createLine (); // 3. generate lines and snowflakes $ this-> createFont (); // 4. generate text $ this-> outPut (); // 5. output Verification code image} // obtain the verification code public function getCode () {return strtolower ($ this-> code );}}
4. Test
Test code:
<? Php/*** Created by PhpStorm. * User: andy * Date: 16-12-22 * Time: */define ('root _ path', dirname (_ FILE _); require_once ROOT_PATH. '/regiondes/ValidateCode. class. php '; $ _ vc = new ValidateCode (); echo $ _ vc-> doImg ();
Generate verification code:
5. Application
<label></label>
The onclick code above is to click the verification code image to automatically refresh the verification code.
Code. php:
<? Php/*** Created by PhpStorm. * User: andy * Date: 16-12-22 * Time: */require substr (dirname (_ FILE _), 0,-7 ). '/init. inc. php '; $ _ vc = new ValidateCode (); echo $ _ vc-> doImg (); $ _ SESSION ['validatecode'] = $ _ vc-> getCode ();
Complete code for the application can be downloaded from the CMS1.0 file in the https://git.oschina.net/andywww/myTest.
6. Summary
No problems were found during the independent test. But when I applied it to the project, I found that I could not generate a verification code image. I found it online, and some said it was in the outPut () function,
Add a line of ob_clean () code before the header ('content-Type: image/png '). This can solve the verification code problem. Although this method is simple, it may cause other data buffering problems, because the db_clean () function is to discard the content in the output buffer.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.