Nice PHP verification code class

Source: Internet
Author: User

<?php//Verification Code Class class validatecode {private  $charset  =  ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 ';     //random factor      private  $code;                             //Verification Code      private  $codelen  = 4;                     //Verification Code length     private  $width  =  130;                     //width     private  $height  = 50;                     //height     private  $img;                                  //Graphics resource handle     private  $font ;                                 //the specified font      private  $fontsize  = 20;                 //Specify font size     private  $fontcolor;                          //Specifies font color     //constructor method initialization     public function __ Construct ()  {         $this->font = root_path. ' /font/elephant.ttf ';     }    //Generate 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 Background &NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP;FUNCTION&NBSP;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-&Gt;img,0, $this->height, $this->width,0, $color);     }    //Generate 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]);         }    }    //generating lines, snowflakes      private function createliNE ()  {        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);        }         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);     }    //Foreign Generation      public function doimg ()  {         $this CREATEBG ();         $this->createcode ();          $this->createline ();         $this CreateFont ();         $this->output ();     }     //Get Verification Code     public function getcode ()  {          Return strtolower ($this->code);     }} 



Nice PHP verification code class

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.