PHP 使用GD库 生成验证码

使用函数 phpinfo();gd_info(); 查看GD 库相关信息
如果没有开启相关扩展 gd_info() 函数将不可用
在 php.ini 配置文件中 找到 ;extension=php_gd2.dll 去掉前面的分号 保存并重启相关服务

利用GD库 生成验证码图片

截个图 字体用的tp5 fonts里面的 6个字体文件
在这里插入图片描述

大致步骤如下:

  • 首先你要有一块画布 创建画布资源 有了画布就可以随意画了
  • 然后给画布定一个基本的调 ( 背景色填充)
    $img = imagecreatetruecolor($width ,$height);//创建画布资源
    $color = imagecolorallocate($img ,220 ,220 ,220);//创建颜色
    imagefill($img ,$color);//填充画布
    
  • 接下来是画一些干扰元素
    • 比如 随机的 干扰点
    	//$color 是要画的点的颜色  x  ,y  表示坐标  $img 画布资源
    	imagesetpixel($img, $x, $y, $color);//画点
    
    • 比如 随机的 干扰线
    	//$color 是线颜色  x1  ,y1 表示线的开始坐标 x2,y2 结束坐标 
    	imageline($img, $x1, $y1, $x2, $y2, $color);//画线
    
    • 比如 随机的 雪花
    	// $font = 字体大小 1-5  $color 雪花颜色, 莫非不是白色
    	$string = '*';//雪花 ^ ^
    	imagestring($img, $font, $x, $y, $string, $color);//画一个字符
    
  • 接下来就是最主要的 生成验证码文字
    //$img 画布资源  $size 字体大小  $angle 字体旋转角度 
    //$fontfile  要使用的字体文件位置  $text  字符
    imagettftext($img, $size, $angle, $x, $y, $color, $fontfile, $text);//文字写入画布
    
  • 最后输出验证码图片
    header('Content-type :image/png');//图片格式 .png
    imagepng($img);//输出
    

好了,贴码 一个灰尘简单的验证码类

<?php
class Verify
{
	protected $string = '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
	private   $width;	//验证码宽
	private   $height;  //高
	protected $img;		//画布资源
	protected $code;	//验证码文字
	protected $num; 	//验证码文字个数
	//protected $config;	//配置项

	public function __construct ($width=160 ,$height=45 ,$num=4)
	{
		$this->width  = $width;
		$this->height = $height;
		$this->num    = $num;
		$this->set_canvas();
		$this->set_disturb();
		$this->set_code();
	}

	//创建画布资源
	protected function set_canvas ()
	{
		$bgColor = [220,220,220];
		$this->img = imagecreatetruecolor($this->width, $this->height);//画布资源
		$color 	  = imagecolorallocate($this->img, $bgColor[0], $bgColor[1], $bgColor[2]);
		imagefill($this->img, 0, 0, $color);//填充画布
	}

	//设置干扰元素
	protected function set_disturb ()
	{
		//if ($this->config['DISTURB_PIXEL']) {//是否开启点干扰
			//生成干扰元素
			  //随机点100个
			for ($i=0; $i<100; $i++) {
				$pixel_color = imagecolorallocate($this->img, mt_rand(50,100), mt_rand(50,100), mt_rand(50,100)); //点颜色
				imagesetpixel($this->img, mt_rand(0 ,$this->width-1), mt_rand(0 ,$this->height-1), $pixel_color);//随机点
			}
		//}
		
		//if ($this->config['DISTURB_SNOW']) {	
			  //生成雪花20个
			for ($i=0; $i<20; $i++) {
				$snow_color = imagecolorallocate($this->img, 255, 255, 255);//雪花颜色
				imagestring($this->img, 3, mt_rand(0 ,$this->width-1), mt_rand(0 ,$this->height-1), '*', $snow_color);//随机位置雪花
			}
		//}

		//if ($this->config['DISTURB_LINE']) {
			  //干扰线10条
			for ($i=0; $i<10; $i++) {
				$line_color = imagecolorallocate($this->img, mt_rand(50,225), mt_rand(50,225), mt_rand(50,225));//线颜色		
				imageline($this->img, mt_rand(0 ,$this->width-1), mt_rand(0 ,$this->height-1), mt_rand(0,$this->width-1), mt_rand(0 ,$this->height-1), $line_color); //随机位置线条
			}
		//}
		
	}

	//生成验证码文字
	protected function set_code ()
	{
		//生成随机字符
		$str = '';
		for ($i=0; $i<$this->num; $i++) {
			if (!$this->config['TYPE']) {//代码走这里
				$code = substr($this->string, mt_rand(0 ,strlen($this->string)-1) ,1);//生成随机字符
			} else { //这里是中文验证码文字  删掉了 一般用不上
				$code = mb_substr($this->config['DICTIONARY'], mt_rand(0 ,mb_strlen($this->config['DICTIONARY'] ,'utf8')-1) ,1 ,'utf8');
			}
			
			$str .= $code; //拼接字符串
			$code_color = imagecolorallocate($this->img, mt_rand(0 ,75), mt_rand(0 ,75), mt_rand(0 ,75));//字体颜色
			$size = mt_rand(20 ,25); //字体大小
			$angle = 0; //旋转角度
			$x = 10+$i*(($this->width)/$this->num); //字符左下角
			$y = mt_rand($size ,$this->height-5); //字符基本线(非最底部)
			$fontfile = FONT_PATH . mt_rand(1,6) . '.ttf'; //字体文件位置 自行修改
			imagettftext($this->img, $size, $angle, $x, $y, $code_color, $fontfile, $code);//写入画布
		}
		
		$this->code = $str;//保存文字
	}

	public function show_verify ()
	{
		//输出验证码
		header('Content-type :image/png');
		imagepng($this->img);
	}

	//返回验证码文字
	public function get_code ()
	{
		return $this->code;
	}

	function __destruct ()
	{
		imagedestroy($this->img);//销毁画布资源
	}
}

还可能用到GD库的php日常
生成缩略图
图片添加水印(水印文字, 水印图片)附:
https://blog.csdn.net/csdn_zhongwu/article/details/85403142

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值