PHP Generate Image Verification code

Source: Internet
Author: User

/** *  verification code class for generating various verification codes  */class captcha{    /**      *  Generate verification Code      *  @param  int  $length   code character length       *  @param  int  $width   width      *  @param   int  $height   Length      *  @param  string  $type   type       *  @return       */    public  Static function getcode ($length =4, $width =50, $height =22, $type = ' png ')      {         //session_start ();          $randval  = self::randstring ($length);         //$_ session[' captcha ']=  $randval;                  $width  =  ($length *9+10) > $width? $length *9+10: $width;        if  (  $type! = ' gif '  && function_exists (' Imagecreatetruecolor '))  {              $im  =  @imagecreatetruecolor ($width, $height);         }else {              $im  =  @imagecreate ($width, $height);         }         $r  = array (225,255,255,223);          $g  = array (225,236,237,255);          $b  = array (225,236,166,125);         $key  = mt_rand (0,3);         $backColor  =  Imagecolorallocate ($im,  $r [$key], $g [$kEY], $b [$key]);     //background color (random)          $borderColor  = imagecolorallocate ($im,  100, 100, 100);                     //Border Color           $pointColor  = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand ( 0,255))             //point color           @imagefilledrectangle ($im, 0, 0,  $width  - 1,  $height  - 1,  $backColor);         @imagerectangle ($im,  0,  0,  $width -1,  $height -1,  $borderColor);         $ Stringcolor = imagecolorallocate ($im, Mt_rand (0,200), Mt_rand (0,120), Mt_rand (0,120));         //  interference         for ($i =0; $i <10; $i + +) {              $fontcolor =imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));             imagearc ($im, Mt_rand ( -10, $width), Mt_rand ( -10, $height), Mt_rand (30,300), Mt_rand (20,200), 55,44, $fontcolor);         }        for ($i =0; $i <25; $i + +) {              $fontcolor =imagecolorallocate ($im, Mt_rand ( 0,255), Mt_rand (0,255), Mt_rand (0,255));             Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pointColor);         }         @imagestring ($im,  10, 20, 10, $ randval,  $stringColor);            header ("content-type: image/". $type );         $ImageFun = ' Image '. $type;          $ImageFun ($im),         imagedestroy ($im);          return  $randval;     }}

The above is the complete method to generate a picture verification code, the following is the use of this method to generate a verification code:

Include ". /lib/captcha.class.php "; $captcha = new Captcha (); $checkCode = $captcha->getcode (4,75,32); Echo $checkCode;

Finally, the effect of the page display is this, refresh one at a time:

PHP Generate Image Verification 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.