知识点 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="" />