Introduction: This is a detailed page of PHP verification code writing class. It introduces related knowledge, skills, experience, and some PHP source code.
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 <? PHP
2 /**
3 * QQ: 279861795
4 * Author: gwyy
5 * Date: 2011-7-01
6 * General Verification Code
7 * version: v0.1
8 */
9
10 class validatecode {
11 private $ charset = "abcdefghizklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890"; // random factor
12 private $ code; // Verification Code text
13 private $ codelen = 4; // Several texts are displayed in the verification code
14 private $ width = 130; // Verification Code width
15 private $ Height = 50; // Verification Code height
16 private $ IMG; // Verification Code resource handle
17 private $ font; // specified font
18 private $ fontsize = 20; // The specified font size
19 private $ fontcolor; // random font color
20
21 // font of the constructor class
22 public function _ construct (){
23 $ this-> 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 a background
35 private function createbg (){
36 // create a canvas for a resource jubing
37 $ 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); // font Length
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 (1, 5 ), $ 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 the header
70 header ('contenttype: img/PNG ');
71 // output image
72 imagepng ($ this-> IMG );
73 // destroy result set
74 imagedestroy ($ this-> IMG );
75}
76 // output externally
77 Public Function doimg (){
78 // load the background
79 $ this-> createbg ();
80 // load the file
81 $ this-> createcode ();
82 // Load Lines
83 $ this-> createline ();
84 // load the font
85 $ this-> createfont ();
86 // load the 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
<? 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.