Php verification code writing class Introduction: This is a php verification code writing class detailed page, introduced and php, related knowledge, skills, experience, and some php source code, etc.
Class = 'pingjiaf' frameborder = '0' src = 'http: // biancheng.dnbc?info/pingjia.php? Id = 341730 'Rolling = 'no'>
I wrote a verification code function some time ago. today I made a verification code class to help object-oriented programming.
Img. php
1
Font = ROOT_PATH. '/font/elephant. ttf'; 24} 25 // Create 4 random codes 26 private function createCode () {27 $ _ leng = strlen ($ this-> charset ); 28 for ($ I = 1; $ I <= $ this-> codelen; $ I ++) {29 $ this-> code. = $ this-> charset [mt_rand (0, $ _ leng)]; 30} 31 return $ this-> code; 32} 33 34 // create background 35 private function createBg () {36 // Create a canvas to a resource jubing37 $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height); 38 // background color 39 $ color = imagecolorallocate ($ This-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255); 40 // draw a rectangle 41 imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color); 42} 43 44 // Create a font 45 private function createFont () {46 $ _ x = ($ this-> width/$ this-> codelen); // The font length is 47 for ($ I = 0; $ I <$ this-> codelen; $ I ++) {48 // text color 49 $ color = imagecolorallocate ($ this-> img, mt_rand (0,156 ), mt_rand (0,156), mt_rand (0,156); 50 // resource handle font size skew font length font Height font color font specific text 51 imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30,30), $ _ x * $ I + mt_rand ), $ this-> height/1.4, $ color, $ this-> font, $ this-> code [$ I]); 52} 53} 54 // random line 55 private function createLine () {56 // random line 57 for ($ I = 0; $ I <6; $ I ++) {58 $ color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); 59 imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color); 60} 61 // random snowflake 62 for ($ I = 0; $ I <45; $ I ++) {63 $ color = imagecolorallocate ($ this-> img, mt_rand (220,255), mt_rand (220,255), mt_rand (220,255 )); 64 imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ),'*', $ color); 65} 66} 67 // outPut background 68 private function outPut () {69 // Generate header 70 header ('contenttype: img/png '); 71 // output Image 72 imagepng ($ this-> img ); 73 // destroy result set 74 imagedestroy ($ this-> img); 75} 76 // output 77 public function doimg () {78 // load the background 79 $ this-> createBg (); 80 // load the file 81 $ this-> createCode (); 82 // load the line 83 $ this-> createLine (); 84 // load the font 85 $ this-> createFont (); 86 // load background 87 $ this-> outPut (); 88} 89 90 // Obtain the verification code 91 public function getCode () {92 return strtolower ($ this-> code ); 93} 94 95} 96 97?>
Other page call methods
Index. php
Require 'IMG. php ';
$ Img = new ValidateCode ();
Echo $ img-> doimg ();
?>
Past ysmoke
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/341730.html pageNo: 6.