<?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->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-
>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->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->HB);
}else{exit ("No such image in GD Library"); }//11.
Assemble private Function Zuhe () { $this->HB ();
$this->bgcolor ();
$this->ganrao ();
$this->huxian ();
$this->xiezi ();
$this->outimg ();
The Public Function Getcodestr () {return $this->codestr; }}?>