很久以前刚学php的时候写的中文验证码功能分享一下

管理员 发布于 4年前   347

很久以前刚学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);

20160930135840356.png

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客