很久以前刚学php的时候写的中文验证码功能分享一下
php  /  管理员 发布于 2年前   593
很久以前刚学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);
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号