Personal write PHP validation code generation class sharing _php instance

Source: Internet
Author: User

This verification code class can be used directly, you can also refer to!

Where the class member Codestr is the generated authentication code string:

<?php/** * Verification Code/Class code{//1. Define members with wide, high, canvas, Word, type, painting type private $width;//width private $height;//height Private $num; Verification code words private $imgType; Generate picture type private $Type; String type 1,2,3 three options 1 pure digit 2 Pure Small Letter 3 Case number mixed private $HB; Canvas public $codestr;
    Validation 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 random fetch string function Private function codestr () {switch ($this->type) {Case 1://Type 1 gets 1-9 random number $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 get number, lowercase letter, uppercase mixed 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 = rand (97,122); 
          Break
        $str. = sprintf ("%c", $o);     
    } break;  
  return $str; }//3. 
  Initialize Canvas image resource Private Function Hb () {$this-&GT;HB = Imagecreatetruecolor ($this->width, $this->height); }//4. 
  Generate background color private function Bg () {return imagecolorallocate ($this->hb,rand (130,250), Rand (130,250), Rand (130,250)); }//5.  
  Generate font Color private function font () {return imagecolorallocate ($this->hb,rand (0,100), Rand (0,100), Rand (0,100)); }//6. Fill background color private function bgcolor () {Imagefilledrectangle ($this->hb,0,0, $this->width, $this->height, $this-& Gt 
  Bg ()); }//7. Noise 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-  
    &GT;BG ()); }//8. Random line arcs 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-&GT;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-&GT;HB); 
    }else{exit ("No such image in GD Library"); }//11.
Assemble private Function Zuhe () {    $this-&GT;HB ();
    $this->bgcolor ();
    $this->ganrao ();
    $this->huxian ();
    $this->xiezi (); 
  $this->outimg ();    
  The Public Function Getcodestr () {return $this->codestr; }}?>

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.