php实现图片验证码的方法:1、加载GD扩展;2、创建画布并在画布上增加内容;3、通过imagepng保存输出;4、释放资源;5、生成随机验证码数据即可。
本文操作环境:Windows7系统、PHP7.1版,DELL G3电脑。
php怎么实现图片验证码?
PHP实现图片验证码功能
验证码: captcha, 是一种用于区别人和电脑的技术
原理(Completely Automated Public Turing Test to Tell Computers and Humans Apart (全自动区分计算机和人类的图灵测试)
如何区分计算机和人类?
只要是文字性的内容,计算机一定可以识别; 计算机是不能识别图片内容的, 但是人眼非常容易识别图片中的内容.
验证码本质: 将文字性的内容印在图片上, 用来实现计算机和人类的区别.
一、图片扩展了解
PHP本身无法操作图片.
PHP但是可以利用提供的图片扩展操作图片.
图片扩展有很多: 常用的是GD
加载GD扩展: 所有的GD扩展函数image开头
二、PHP操作图片
1.增加画布(创建画布)
图片资源 imagecreatetruecolor(宽,高);
2.在画布上增加内容(文字)
a)给将要在图片上添加的内容分配颜色: 先将颜色关联到图片资源,然后才可以使用
颜色句柄[整型] imagecolorallocate(图片资源,红色,绿色,蓝色); //颜色可以使用数字0-255或者使用十六进制#十六进制
b)写文字: 只能写英文(ASCII码表上的内容)
布尔 imagestring(图片资源,文字大小,起始X坐标,起始Y坐标,写内容,颜色);
字体大小: 1-5
3.保存输出
imagepng(图片资源[,保存位置]);
4.释放资源(资源建议释放)
布尔结果 imagedestroy(图片资源);
//1. 创建画布 $img = imagecreatetruecolor(200,200); //var_dump($img); //2. 作画 //2.1 给画布分配颜色 $color = imagecolorallocate($img,255,255,255); //echo $color; //2.2 写入文字 $true = imagestring($img,5,50,90,'hello world',$color); //var_dump($true); //3. 保存输出内容 //3.1 输出 //告诉浏览器,内容是图片 //header('Content-type:image/png'); //imagepng($img); //3.2 保存图片 imagepng($img,'hello.png'); // 保存到当前目录 //4. 释放资源 $res = imagedestroy($img); var_dump($res);
三、验证码图片
1.生成随机验证码数据
2.创建画布
3.填充背景色: imagefill(图片资源,起始位置X,起始位置Y,颜色句柄); //imagefill: 找到一个像素点之后, 如果发现周围相邻的像素点与当前像素点颜色一样(全部黑色)就会被自动渲染.
4.添加文字内容: 先分配颜色
5.保存输出: 验证码都是输出
6.释放资源
//制作验证码图片 //获取验证码字符串 $captcha = ''; for($i = 0;$i < 4;$i++){ //chr: 将数字转换成对应的字符(ASCII) switch(mt_rand(0,2)){ case 0: //数字 $captcha .= chr(mt_rand(49,57)); break; case 1: //大写字母 $captcha .= chr(mt_rand(65,90)); break; case 2: //小写字母 $captcha .= chr(mt_rand(97,122)); break; } } //echo $captcha; //创建画布 $img = imagecreatetruecolor(200,200); //给背景分配颜色 $bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); //填充背景色 imagefill($img,0,0,$bg); //循环写入 for($i = 0;$i < 4;$i++){ //分配文字颜色 $txt = imagecolorallocate($img,mt_rand(50,150),mt_rand(50,150),mt_rand(50,150)); //写入文字 imagestring($img,mt_rand(1,5),60 + $i*20,90,$captcha[$i],$txt); } //输出 header('Content-type:image/png'); imagepng($img); //释放资源 imagedestroy($img);
四、中文验证码
两个注意点
获取随机中文: 在PHP中都是以字节为单位操作数据,中文在不同字符集中有不同字节数
中文写入函数: imagettftext(图片资源,字体大小, 字体旋转角度, 字体的起始X,字体起始Y, 字体文件,内容, 颜色);
1.创建画布: 填充背景色
2.获取随机中文
3.将中文写入到图片
4.保存输出图片
5.销毁资源
//中文验证码 header('Content-type:text/html;charset=utf-8'); //创建画布 $img = imagecreatetruecolor(200,200); //给背景分配颜色 $bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); //填充背景色 imagefill($img,0,0,$bg); //写入内容 for($i = 0;$i < 4;$i++){ //分配颜色 $txt = imagecolorallocate($img,mt_rand(50,150),mt_rand(50,150),mt_rand(50,150)); //获取随机中文 $string = "今天我寒夜里看雪飘过怀着冷却了的心窝飘远方"; $pos = mt_rand(0,strlen($string) - 1); $start = $pos - $pos % 3; //utf-8取模3,GBK取模2 //取三个长度(字符串截取) $target = substr($string,$start,3); //写入中文 imagettftext($img,mt_rand(20,40),mt_rand(-45,45),40 + $i * 30, mt_rand(80,120),$txt,'simple.ttf',$target); } //输出图片 header('Content-type:image/png'); imagepng($img); //销毁资源 imagedestroy($img);
五、封装验证码类
//验证码工具类 class Captcha{ //属性 private $width; private $height; private $strlen; private $lines; //干扰线数量 private $stars; //干扰点数量 private $font; //字体路径 //构造方法:初始化属性 public function __construct($info = array()){ //初始化属性 $this->width = isset($info['width'])?$info['width']:146; $this->height = isset($info['height'])?$info['height']:23; $this->strlen = isset($info['strlen'])?$info['strlen']:4; $this->lines = isset($info['lines'])?$info['lines']:10; $this->stars = isset($info['stars'])?$info['stars']:50; $this->font = isset($info['font'])?$info['font']:'fonts/AxureHandwriting-BoldItalic.otf'; } //生成验证码图片 public function generate(){ //创建画布,给定背景色 $img = imagecreatetruecolor($this->width,$this->height); $c_bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); imagefill($img,0,0,$c_bg); //写入字符串 $captcha = $this->getStr(); //增加干扰点"*" for($i = 0;$i < $this->stars;$i++){ //随机颜色 $c_star = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150)); //写入*号 imagestring($img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$c_star); } //增加干扰线 for($i = 0;$i < $this->lines;$i++){ //随机颜色 $c_line = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200)); //划线 imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$c_line); } //随机颜色 for($i = 0;$i < $this->strlen;$i++){ $c_str = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); imagettftext($img,mt_rand(10,20),mt_rand(-45,45),20 + $i * 30,mt_rand(14,$this->height - 6),$c_str,$this->font,$captcha[$i]); } //输出图片 header('Content-type:image/png'); imagepng($img); //释放资源 imagedestroy($img); } //获取随机字符串 //@return 字符串 private function getStr(){ //ASCII码表生成字符串 $str = ''; //循环生成 for($i = 0;$i < $this->strlen;$i++){ //随机选择数字,小写字母和大写字母 switch(mt_rand(0,2)){ case 0: //数字 $str .= chr(mt_rand(49,57)); break; case 1: //小写字母 $str .= chr(mt_rand(97,122)); break; case 2: //大写字母 $str .= chr(mt_rand(65,90)); break; } } //将验证码字符串保存到session $_SESSION['captcha'] = $str; //返回结果 return $str; } /* * 验证验证码 * @param1 string $captcha,用户输入的验证码数据 * @return boolean,成功返回true,失败返回false */ public static function checkCaptcha($captcha){ //与session中的验证码进行验证 //验证码不区分大小写 return (strtoupper($captcha) === strtoupper($_SESSION['captcha'])); } }
推荐学习:《PHP视频教程》
以上是php怎么实现图片验证码的详细内容。更多信息请关注PHP中文网其他相关文章!

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器