做中文验证码

知识点 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);
<img src="https://img-blog.csdn.net/20160930135840356?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif;" alt="" />

博客:http://www.zongscan.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值