<? Php Class Code { // 1. Define the width, height, canvas, word count, type, and painting type of each member. Private $ width; // width Private $ height; // height Private $ num; // number of characters in the verification code Private $ imgType; // type of the generated image Private $ Type; // string Type: 1, 2, 3, three options: 1, numbers, 2, lowercase letters, 3, uppercase, lowercase letters, and numbers Private $ hb; // canvas Public $ codestr; // verification code string Public function _ construct ($ height = 20, $ num = 4, $ imgType = "jpeg", $ Type = 1 ){ $ This-> width = $ num * 20; $ This-> height = $ height; $ This-> num = $ num; $ This-> imgType = $ imgType; $ This-> Type = $ Type; $ This-> codestr = $ this-> codestr (); $ This-> zuhe (); } // 2. Define a random string Function Private function codestr (){ Switch ($ this-> Type ){ Case 1: // type 1 to obtain a random number from 1 to 9 $ Str = implode ("", array_rand (range (0, 9), $ this-> num )); Break; Case 2: // type 2 get a-z random lowercase letter $ Str = implode ("", array_rand (array_flip (range (a, z), $ this-> num )); Break; Case 3: // Type 3: Numbers, lowercase letters, and uppercase letters For ($ I = 0; $ I <$ this-> num; $ I ++ ){ $ M = rand (0, 2 ); Switch ($ m ){ Case 0: $ O = rand (48, 57 ); Break; Case 1: $ O = rand (65,90 ); Break; Case 2: $ O = revert (97,122 ); Break; } $ Str. = sprintf ("% c", $ o ); } Break; } Return $ str; }
// 3. initialize canvas image resources Private function Hb (){ $ This-> hb = imagecreatetruecolor ($ this-> width, $ this-> height ); }
// 4. Generate the background color Private function Bg (){ Return imagecolorallocate ($ this-> hb, rand (130,250), rand (130,250), rand (130,250 )); } // 5. Generate the font color Private function Font (){ Return imagecolorallocate ($ this-> hb, rand (0,100), rand (0,100), rand (0,100 )); } // 6. Fill in the background color Private function BgColor (){ Imagefilledrectangle ($ this-> hb, 0, $ this-> width, $ this-> height, $ this-> Bg ()); } // 7. Interference point Private function ganrao (){ $ Sum = floor ($ this-> width) * ($ this-> height)/3 ); For ($ I = 0; $ I <$ sum; $ I ++ ){ Imagesetpixel ($ this-> hb, rand (0, $ this-> width), rand (0, $ this-> height), $ this-> Bg ()); } } // 8. Random line arc Private function huxian (){ For ($ I = 0; $ I <$ this-> num; $ I ++ ){ ImageArc ($ this-> hb, rand (0, $ this-> width), rand (0, $ this-> height), rand (0, $ this-> width), rand (0, $ this-> height), rand (0,360), rand (0,360), $ this-> Bg ()); } } // 9. Write Private function xiezi (){ For ($ I = 0; $ I <$ this-> num; $ I ++ ){ $ X = ceil ($ this-> width/$ this-> num) * $ I; $ Y = rand (1, $ this-> height-15 ); Imagechar ($ this-> hb, 5, $ x + 4, $ y, $ this-> codestr [$ I], $ this-> Font ()); } } // 10. Output Private function OutImg (){ $ Shuchu = "image". $ this-> imgType; $ Header = "Content-type: image/". $ this-> imgType; If (function_exists ($ shuchu )){ Header ($ header ); $ Shuchu ($ this-> hb ); } Else { Exit ("GD library does not have such images "); } } // 11. Assemble Private function zuhe (){ $ This-> Hb (); $ This-> BgColor (); $ This-> ganrao (); $ This-> huxian (); $ This-> xiezi (); $ This-> OutImg (); } Public function getCodeStr (){ Return $ this-> codestr; } } ?> |