The class that generates the verification code under Laravel. laravel generates the verification code.
<? Phpnamespace App \ Tool \ Validate; // Verification code class ValidateCode {private $ charset = 'hangzhou'; // random factor private $ code; // Verification code private $ codelen = 4; // verification code length: private $ width = 130; // width: private $ height = 50; // height: private $ img; // graphic resource handle: private $ font; // specify the font private $ fontsize = 20; // specify the font size private $ fontcolor; // specify the font color // initialize public function _ construct () {$ this-> font = public_path (). '/fonts/Elephant. ttf'; // note that the font path must be correct; otherwise, the image $ this-> createCode ();} // generate a 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 () {$ 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 the 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), $ _ x * $ I + mt_rand (1, 5), $ this-> height/1.4, $ this-> fontcolor, $ this-> font, $ this-> code [$ I]);} // generate the line and snowflake private function createLine () {// line 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-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ), $ color) ;}// snowflake for ($ I = 0; $ 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 );} // generate public function doimg () {$ this-> createBg (); $ this-> createLine (); $ this-> createFont (); $ this-> outPut ();} // obtain the verification code public function getCode () {return strtolower ($ this-> code );}}