PHP Verification Code generation class complete code

Source: Internet
Author: User
Tags lowercase rand
The code is as follows Copy Code

<?php
Class code{

1. Define members with wide, high, canvas, Word, type, painting type

Private $width; Width
Private $height; Height
Private $num; Verification code number of words
Private $imgType; Build 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; Verifying Code strings

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. To define a random fetch string function
Private Function Codestr () {
Switch ($this->type) {

Case 1://Type 1 get 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. Initializing Canvas image Resources
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->bg ());
}

7. Jamming 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 Straight line 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->HB);
}else{
Exit ("No such image in GD Library");
}
}

11. Assembling
Private Function Zuhe () {
$this->HB ();
$this->bgcolor ();
$this->ganrao ();
$this->huxian ();
$this->xiezi ();
$this->outimg ();
}

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.