很久以前刚学php的时候写的中文验证码功能分享一下
php  /  管理员 发布于 5年前   1046
很久以前刚学php的时候写的中文验证码功能代码逻辑,搬过来填充数据。
知识点: imagettftext — 用 TrueType 字体向图像写入文本(具体参数看手册)
如何产生随机的中文字符串 中文安其uniccode编码 是有规律的 位于 0x4e00-0x9fa0
但是请注意 对于客户来说 能否认识 ,所以在实际项目中 只是抽取几百或上千的常用汉字 放数组里 随机选取
//选常用字体 放数组中
$char = array('中','华','人','民','共','和','国','我','是','湖','南','郴','州');
//打乱
shuffle($char);
//随机选取
$code = implode('',array_slice($char, 0,4));
//画布
$im = imagecreatetruecolor(68, 30);
//创建颜料
$gray = imagecolorallocate($im, 200,200,200);
$blue = imagecolorallocate($im, 0,0,255);
//随机颜色
$randcolor = imagecolorallocate($im, mt_rand(0,150), mt_rand(0,150),mt_rand(0,150));
$randcolor1 = imagecolorallocate($im, mt_rand(0,150), mt_rand(0,150),mt_rand(0,150));
$randcolor2 = imagecolorallocate($im, mt_rand(0,150), mt_rand(0,150),mt_rand(0,150));
//随机线
imageline($im, 0, mt_rand(0,30), 68, mt_rand(0,30), $randcolor);
imageline($im, 0, mt_rand(0,30), 68, mt_rand(0,30), $randcolor1);
imageline($im, 0, mt_rand(0,30), 68, mt_rand(0,30), $randcolor2);
//填充背景
imagefill($im, 0, 0, $gray);
//写字字体设置
imagettftext($im, 12, 0, 2, 20, $blue, './ch/msyh.ttf', $code);
//告诉浏览器格式
header('content-type: image/jpeg');
//显示图像
imagejpeg($im);
//销毁资源
imagedestroy($im);

test1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
