php验证码的编写

所需要掌握的知识:oop编程思想,以及php验证码的基本函数的了解


使用oop的思想来编写验证码程序,实现良好的封装,与改写

 首先需要理解验证码编写的一般程序:

              验证码字符串,code_str;

              验证码的默认长度:code_len;

              验证码的字体颜色:font_color;

              验证码的字体大小:font_color;

              画布的大小: imagecreatetruecolor(width,height);

              画布的背景颜色:bg_color;

              Imagecolorallocate();给一幅图片分配颜色

              Imagefill(image,x,y,color);填充图片的背景颜色;

              $bg_color= imagecolorallocate($img,

           hexdec(substr($bg_color, 1,2)),hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2)));

           imagefill($img, 0, 0, $bg_color);

              通过hexdec来将16进制的数转化成10进制;

              Imagettftext(img.x,y);


   
   font= "font".DIRECTORY_SEPARATOR.'ariblk.ttf';
	}

		//产生验证码
	private function create_code(){
		$code = '';
		for($i=0; $i < $this->code_len; $i++){
			$code.=$this->code_str[mt_rand(0,strlen($this->code_str)-1)];
			//$this->code .= $this->code_str[mt_rand(0,strlen($this->code_str)-1)];
		}
		 	$this->code = $code;
	}

		//得到验证码图片
/*	public function getimage(){
		$w = $this->width;
		$h = $this->height;
		$bg_color = $this->bg_color;
		if(!$this->checkgd())return false;
		$img = imagecreatetruecolor($w, $h);    //新建画布
		$bg_color = imagecolorallocate($img,hexdec(substr($bg_color,1,2)),    //背景颜色,hexdec由16进制转化到10进制
			hexdec(substr($bg_color,3,2)),hexdec(substr($bg_color,5,2)));
		imagefill($img, 0, 0, $bg_color);			//颜色填充

		$this->img = $img;						//调用该属性	
		$this->create_font();	
		$this->create_pix();
		$this->show_code();
	}
*/

	public function getimage(){
		$w = $this->width;
		$h = $this->height;
		$bg_color= $this->bg_color;
		if(!$this->checkgd())return false;
		$img = imagecreatetruecolor($w, $h);
	    $bg_color = imagecolorallocate($img, 
	    hexdec(substr($bg_color, 1,2)), hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2)));
	    imagefill($img, 0, 0, $bg_color);
	    $this->img = $img;
	    $this->create_font();
	    $this->create_pix();
	    $this->show_code();
	}
	
	//写入验证码
	private function create_font(){
		$this->create_code();
		$color= $this->font_color;
		$font_color = imagecolorallocate($this->img, 
	    hexdec(substr($color, 1,2)), hexdec(substr($color, 3,2)), hexdec(substr($color, 5,2)));
	     $x = $this->width/$this->code_len;
	    for($i=0;$i<$this->code_len;$i++){
	    	imagettftext($this->img, $this->font_size, mt_rand(-30, 30), $x*$i+mt_rand(3,6), mt_rand($this->height/1.2,$this->height-5), $font_color, $this->font, $this->code[$i]);
	    }
	    $this->font_color = $font_color;
	}
	/*public function create_font(){
			$this->create_code();   		
			$code_len = $this->code_len;
			$color = $this->font_color;
			$x = $this->width/$code_len;
			$font_color = imagecolorallocate($this->img,hexdec(substr($color,1,2)), 
			hexdec(substr($color,3,2)),hexdec(substr($color,5,2)));
			//$font_size = $this->font_size;
			
			for( $i=0; $i < $this->code_len; $i++ ){
				imagefttext($this->img, $this->font_size, mt_rand(-30,30), $x*$i+mt_rand(3,6), mt_rand($this->height/1.2,$this->height-5), 
					$font_color, $this->font, $this->code[$i]);
			}

			 $this->font_color = $font_color;
	    }
	*/
			
	
	//return 验证码
	public function getcode(){
		return strtolower($this->code);
	}
	
	

	//画线 画点
	/*
	private function create_pix(){
			$pix_color = $this->font_color;
			for( $i=0; $i < 100; $i++ ){
				imagesetpixel($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height), $pix_color);
			}

			for ($j=0; $j < 2 ; $j++) { 
				imagesetthickness($this->img,mt_rand(1, 2));
				imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height),  
					mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
			}
	}
*/

private function create_pix(){
		$pix_color= $this->font_color;
		for($i=0;$i<100;$i++){
			imagesetpixel($this->img, mt_rand(0, $this->width),mt_rand(0, $this->height), $pix_color);
		}
		for($j=0;$j<2;$j++){
			imagesetthickness($this->img, mt_rand(1, 2));
			imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height),  mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
		}
	}

	//显示验证码
	private function show_code(){
		header("Content-type:image/png");
		imagepng($this->img);
		imagedestroy($this->img);
	}

	//检测gd库是否支持和imagepng是否存在
/*	public function check(){
		if( function_exists('imagepng') && extension_loaded('gd'));
	}
*/	

	private function checkgd(){
		return extension_loaded('gd')&&function_exists("imagepng");
	}	
}	
?>
<?php 
include('');   //填入你的文件名

//实例化一个对象
$code = new code_yz();
$code->getimage();
?>
查看验证码

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值