<?php/** * Created by Phpstorm. * User:administrator * DATE:2016/6/20 * time:14:29 */class captcha{//Verification code class protected $str = "XAAXQWE556232ASSD" ; Random number protected $code; Verification Code protected $length = 4; Verification code Length protected $width = 80; Verification code Width Protected $height = 30; Verification code height protected $img;//verification code generation//Random number Public Function GetCode () {$len =strlen ($this->str)-1; for ($i =0; $i <4; $i + +) {$this->code.= $this->str[mt_rand (0, $len)]; }}//Generate background Public function CODEBG () {//Create a new image $this->img=imagecreatetruecolor ($this->width , $this->height); $color =imagecolorallocate ($this->img,rand (1,100), rand (1,100), rand (1,100)); Red, green, blue//$back =imagecolorallocate ($this->img,0,0,0); Imagefilledrectangle ($this->img,0,0, $this->width, $this->height, $color); }//Generate interference element public function Setline () {//Interference point for ($i =0; $i <1000; $i + +) { $color =imagecolorallocate ($this->img,rand (0,255), Rand (0,255), Rand (0,255)); Imagesetpixel ($this->img,rand (1,99), Rand (1,99), $color); }//////Interference line for ($i =0; $i <5; $i + +) {$color =imagecolorallocate ($this->img,rand (0,255), R and (0,255), Rand (0,255)); Imageline ($this->img,rand (0, $this->width), rand (0, $this->height), rand (0, $this->width), rand (0, $this- >height), $color); }//////Interference line}//Generate element Header public Function Outheader () {Header ("content-type:image/png"); }//write Verification code Public Function WriteString () {$red = Imagecolorallocate ($this->img, 255, 0, 0); Imagestring ($this->img,5,rand (1,15), Rand (1,15), $this->code (), $red); $font = ' Arial.ttf '; Imagettftext ($this->img, 0, ten, $red, $font, $this->code); }//shu Public Function png () {$this->outheader (); $this->CODEBG (); $this->getcode (); $this->setline (); $this->writestring (); Session_Start (); $_session[' code ']= $this->code (); Imagepng ($this->img); Public Function code () {return strtoupper ($this->code); }} $ce =new captcha (); $ce->png (); Var_dump ($_session[' code ');
Test page
<! DOCTYPE html>
PHP Verification Code Class