Verification code Such a function can be said to be everywhere, and then use PHP to implement the verification code such functions, here I was to extract the verification code implementation of a class independent, then if you use the code function, directly introduced the class file and create an instance of the class, you can use the verification code, the code is as follows:
Verification code class file vcode.class.php
Width= $width, $this->height= $height, $this->num= $num, $this->code= $this->createcode (), $this pointnum=100, $this->linenum=10; $this->fontfile= "Stliti. TTF ";} /** * Used to set member properties * @param string $key member property name * @param mixed $value member property value * @return object returns its own $this, which can be used for coherent operation */public Funct Ion Set ($key, $val) {//get_class_vars () gets the array of properties in the class//get_class () returns the class name of the object if (Array_key_exists ($key, Get_class_vars ( Get_class ($this))) {$this->setoption ($key, $val);} return $this;} Set parameters Private Function SetOption ($key, $value) {$this, $key = $value;} Get the Captcha public Function GetCode () {return $this->code;} Output image Public Function outimg () {//Create image $this->createimage ();//Draw Verification Code $this->drawcode ();//Draw interfering element $this-> Drawdisturbcolor ();//Output Image $this->printimg ();} Draw Verification Code Private Function Drawcode () {$this->fontfile= "./font/". $this->fontfile;for ($i =0; $i < $this->num;$ i++) {//Set random color $randcolor=imagecolorallocate ($this->img,rand (0,128), Rand (0,128), Rand (0,128));//font size $fontsize= Rand (20,23);//font horizontal position $x= ($this->width/$this->num) * $i;//Horizontal position $y=rand ($fontSize, Imagefontheight ($fontSize) +3);//Drawing Font Imagettftext ($this- >img, $fontSize, 0, $x, $y, $randColor, $this->fontfile, $this->code{$i});} Draw interference elements Private function drawdisturbcolor () {//Draw interference points for ($i =0; $i < $this->pointnum; $i + +) {//Set random color $randcolor= Imagecolorallocate ($this->img,rand (0,255), Rand (0,255), Rand (0,255));//Draw Point Imagesetpixel ($this->img,rand (1,$ THIS->WIDTH-2), rand (1, $this->height-2), $randColor);} Draw interference lines for ($i =0; $i < $this->linenum; $i + +) {//Set random color $randcolor=imagecolorallocate ($this->img,rand (0,200), Rand (0,200), Rand (0,200));//Draw line Imageline ($this->img,rand (1, $this->width-2), rand (1, $this->height-2), Rand (1, $this->height-2), rand (1, $this->width-2), $randColor);}} Create Image Private Function createimage () {//Create a true Color image $this->img=imagecreatetruecolor ($this->width, $this height);//Set Background color $bgcolor=imagecolorallocate ($this->img,rand (200,255), Rand (200,255), Rand (200,255));// Fill background color Imagefill ($this->img,0,0, $bgColor);/Set Border color $bordercolor=imagecolorallocate ($this->img,0,0,0);//Draw a border Imagerectangle ($this->img,0,0, $this Width-1, $this->height-1, $borderColor);} Output image Private Function printimg () {if (Imagetypes () & Img_png) {//For Pngheader ("Content-type:image/png"); Imagepng ( $this->img);} else if (Imagetypes () & img_jpg) {//For Jpgheader ("Content-type:image/jpeg"); imagejpeg ($this->img,null,100);} else if (Imagetypes () & Img_gif) {//For Gifheader ("Content-type:image/gif"); Imagegif ($this->img);} else if (Imagetypes () & img_wbmp) {//For Wbmpheader (' content-type:image/vnd.wap.wbmp '); Imagewbmp ($this->img);} Else{die (' No image ' in this PHP server ');}} Create Verification Code Private Function Createcode () {//default string $codes= "123456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY"; /Generate Verification Code $code= ""; for ($i =0; $i < $this->num; $i + +) {$code. = $codes {rand (0,strlen ($codes)-1)};} return $code;} Destructors are used to destroy image resources function __destruct () {Imagedestroy ($this->img);} }
Here I use the font of the function is Imagettftext (), because this function can customize the font style, from the code can also be seen, passed parameters have a font file attributes, if you do not like using this function can use the imagestring () function also line, It's just that the default font size for this function doesn't look good. Or customize the font to look more comfortable. The next step is to invoke the Captcha class
GetCode ();//$vcode->set ("Pointnum", 10)////$vcode->set ("LineNum", 10)///custom number of interference lines//$vcode->set ( "Fontfile", "Wawa.ttf");//Custom font file//output image $vcode->outimg ();
The code is implemented here, and a direct call to the file will see the verification code, using a simple login form
The effect of the implementation: