GD库 验证码类的封装

7 篇文章 0 订阅
2 篇文章 0 订阅

慕课网后 端开发PHP教程-->PHP进阶篇--GD库图像处理-->http://www.imooc.com/learn/701-->2-6 验证码类的封装

<?php
header("content-type:text/html;charset=utf-8");
//error_reporting(0);
Class Captcha{
	//字体文件
	private $_fontfile='';
	//字体大小
	private $_size=20;
	//画布宽度
	private $_width=120;
	//画布高度
	private $_height=40;
	//验证码长度
	private $_length=4;
	//干扰元素
	//雪花*的个数
	private $_snow=0;
	//像素个数
	private $_pixel=0;
	//线段个数
	private $_line=0;

	//画布资源
	private $_image=null;

	//初始化数据
	public function __construct ($config=array()){

		if(is_array($config)&&count($config)>0){
			//检测字体文件是否存在并且可读
			if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
				$this->_fontfile=$config['fontfile'];
			}else{
				return false;
			}
			//检测是否设置字体大小
			if(isset($config['size'])&&$config['size']>0){
				$this->_size=(int)$config['size'];
			}
			//检测是否设置画布宽和高
			if(isset($config['width'])&&$config['width']>0){
				$this->_width=(int)$config['width'];
			}
			if(isset($config['height'])&&$config['height']>0){
				$this->_height=(int)$config['height'];
			}
			//检测是否设置验证码长度
			if(isset($config['length'])&&$config['length']>0){
				$this->_length=(int)$config['length'];
			}
			//配置干扰元素
			if(isset($config['snow'])&&$config['snow']>0){
				$this->_snow=(int)$config['snow'];
			}
			if(isset($config['pixel'])&&$config['pixel']>0){
				$this->_pixel=(int)$config['pixel'];
			}
			if(isset($config['line'])&&$config['line']>0){
				$this->_line=(int)$config['line'];
			}

			
			$this->_image=imagecreatetruecolor($this->_width,$this->_height);
			return $this->_image;

		}else{
			return false;
		}
	}
	//得到验证码
	public function getCaptcha(){
		$white=imagecolorallocate($this->_image,255,255,255);
		//填充矩形
		imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
		//生成验证码
		$str=$this->_generateSre($this->_length);
		if(false===$str){
			return false;
		}
		//绘制验证码
		$size=$this->_size;
		$fontfile=$this->_fontfile;
		for($i=0;$i<$this->_length;$i++){
			$angle=mt_rand(-15,15);
			$x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
			$y=ceil($this->_height/1.5);
			$color=$this->_getRandColor();
			$text=$str[$i];
			imagettftext($this->_image,$size,$angle,$x,$y,$color,$fontfile,$text);
		}

		//像素和线段
		if($this->_snow){
			//使用雪花当做干扰元素
			$this->_getSnow();
		}else{
			if($this->_pixel){
				$this->_getPixel();
			}
			if($this->_line){
				$this->_getLine();
			}
		}
		//echo $str;exit;
		//输出图像
		header("content-type:image/png");
		imagepng($this->_image);
		imagedestroy($this->_image);
		return strtolower($str);
	}
	//产生雪花
	private function _getSnow(){
		for($i=1;$i<=$this->_snow;$i++){
			$color=$this->_getRandColor();
			imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$color);
		}
	}
	//绘制像素
	private function _getPixel(){
		for($i=1;$i<=$this->_pixel;$i++){
			$color=$this->_getRandColor();
			imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
		}
	}
	//绘制线段
	private function _getLine(){
		for($i=1;$i<=$this->_line;$i++){
			$color=$this->_getRandColor();
			imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
		}
	}

	//产生验证码字符
	private function _generateSre($length=4){
		if($length<1||$length>30){
			return false;
		}
		$chars=array(
			'a','b','c','d','e','f','g','h','k','m','n','p','r','s','t','u','v','w','x','y','z',
			'A','B','C','D','E','F','G','H','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
			1,2,3,4,5,6,7,8,9
		);
		$str=str_shuffle(join('',array_rand(array_flip($chars),$length)));
		return $str;
	}
	//产生随机颜色
	private function _getRandColor(){
		return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
	}
}


?>


测试使用

<?php
include("Captcha.class.php");

$config=array(
	'fontfile'=>'fonts/7.ttf',
	'pixel'=>100,
	'line'=>5
	);

$captcha=new Captcha($config);
session_start();
$_SESSION['verifyCode']=$captcha->getCaptcha();

?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值