PHP Implementation Verification Code class

Source: Internet
Author: User
This article mainly introduces the use of PHP to implement verification code class, has a certain reference value, now share to everyone, the need for friends can refer to

<?phpclass code{//1. Define each member has width, height, canvas, word number, type, draw type private $width;//width private $height;//height private $num;//verification Code words private $i Mgtype; Generate picture type private $Type; String type three options 1 pure digit 2 Pure lowercase Letter 3 Case number mixed private $HB; Canvas public $codestr; Verify 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. Defines a random get 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 gets a-Z random lowercase letter $str = Implode ("", Array_rand (Array_flip (range (a,z)), $ This->num)); Break;case 3://Type 3 get numbers, lowercase letters, 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); } $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. Disturbance 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 straight arc 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-&GT;HB);} Else{exit ("GD library does not have such an image");}} 11. Assemble private Function Zuhe () {$this-&GT;HB (); $this->bgcolor (); $this->ganrao (); $this->huxian (); $this Xiezi (); $this->outimg (); }public function Getcodestr () {return $this->codestr;}} $a = new Code (); $a->getcodestr ();? >

Related recommendations:

Using PHP to implement a single linked list

PHP implementation of the Fibonacci sequence

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.