The class that generates the verification code under Laravel. laravel generates the verification code.

Source: Internet
Author: User

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 );}}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.