PHP Verification code example (with refresh) demo_php Picture Verification Code class instance

Source: Internet
Author: User

The

directly introduces the class file and creates an instance of the class, you can use the Authenticode, and the authentication code class file vcode.class.php code is as follows

Verification code class class vcode{private  $width;//Picture width private  $height;//Picture height private  $num;//number of authentication codes private& $IMG///Picture resource private  $code//private  $pointNum, number of interference points private  $lineNum;//number of interference lines private   $fontFile//font file  //constructor initialization related data function __construct ($width =85, $height =34, $num =4) {$this->
		Width= $width;
		$this->height= $height;
		$this->num= $num;
		$this->code= $this->createcode ();
		$this->pointnum=100;
		$this->linenum=10; $this->fontfile= "<a href=" http://dwtedx.com/font/  target= "_blank"  class= "Keylink" > Stl</a>iti.
	TTF ";  //For setting member properties//@param  string  $key   member property name//@param  mixed  $value   Member property value//@return & nbsp;object  returns its own object $this, which can be used for coherent operations Public function set ($key, $val) {//get_class_vars ()   Gets an array of properties in the class//get_class ()   Returns the object's class name if (Array_key_exists ($key, Get_class_vars (Get_class ($this))) {$this-> SetOption ($key, $Val);
	} return  $this;
	}//Set parameter Private function setoption ($key, $value) {$this-> $key = $value;
	}//Get Authenticode Public function getcode () {return  $this->code;
		 //Output image public function outimg () {//Create image $this->createimage ();
		Draw Verification Code $this->drawcode ();
		Draw interference elements $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;
			The position of the horizontal direction $y =rand ($fontSize, Imagefontheight ($fontSize) +3);
		Draw Font Imagettftext ($this->img, $fontSize, 0, $x, $y, $randColor, $this->fontfile, $this->code{$i}); ///Draw Interference element Private function drawdisturbcolor () {//Draw interference point for ($i =0 $i < $this->pointnum; $i + +) {//Set random color $randColor =imagecolorallocate ($this->img,rand (0,255), Rand (0,25
			5), Rand (0,255));
		Draw Point Imagesetpixel ($this->img,rand (1, $this->width-2), rand (1, $this->height-2), $randColor); //Draw Interference line 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-&
		Gt;width, $this->height);
		Sets the background color $bgColor =imagecolorallocate ($this->img,rand (200,255), Rand (200,255), Rand (200,255));
		Fill background color Imagefill ($this->img,0,0, $bgColor);
		Sets the 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 PNG header
			("Content-type:image/png");
		Imagepng ($this->img);
			}else if (Imagetypes ()  & img_jpg) {//For JPG header ("Content-type:image/jpeg");
		Imagejpeg ($this->img,null,100);
			}else if (Imagetypes ()  & img_gif) {//is for the GIF header ("Content-type:image/gif");
		Imagegif ($this->img); }else if (Imagetypes ()  & img_wbmp) {//  for  wbmp header (′content-type: image/
			vnd.wap.wbmp′);
		Imagewbmp ($this->img);
		}else{die (′no image support in this php server′); }  //Create Authenticode Private function createcode () {//default string $codes = 123456789abcdefghijkmnpqrstuvwxyABCDEF
		Ghijklmnopqrstuvwxy ";
		Generate the verification code $code = "";
		For ($i =0 $i < $this->num; $i + +) {$code. = $codes {rand (0,strlen ($codes)-1)}; } REturn  $code;
	The  //destructors are used to destroy image resources Function __destruct () {Imagedestroy ($this->img); }
}

The function of the font that I use here is Imagettftext (), because this function can customize the font style, can also be seen from the code

The passed parameter has a font file attribute, and if you do not like using this function, you can use the imagestring () function.

I just feel like the default font size of this function, it's not good-looking, it's a custom font. Look comfortable.

Call the authentication code class image_002.php code as follows

Open Sessionsession_start ();//Introduce the authentication code class include ("vcode.class.php");//Create the Authentication code class $vcode=new Vcode ()//Save the acquired Captcha to the session $_ session[′code′]= $vcode->getcode ()//$vcode->set ("Pointnum", 10);///$vcode->set ("LineNum", 10) ///$vcode->set ("Fontfile", "Wawa.ttf");//Custom font file//output image $vcode->outimg ();

The HTML code is as follows

Here the whole function is done, hope for some friends useful, but also when they make a note

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.