中文验证码

要想显示中文可以用表示中文字符的Unicode编码的数值范围,但是这样会有许多生僻字,所以定义一个中文字符的数组是更好的解决办法,使用imagettftext()函数可以显示中文字符,但是要引入所要显示文字字体的路径,在C:WindowsFonts有这样的文件,复制到指定文件夹 array imagettftext  ( resource $image  , float $size  , float $angle  , int $x  , int $y  , int $color  , string $fontfile  , string $text  ); 返回数组 函数名imagettftext(画布资源,字体大小,逆时针旋转的角度,第一个字符左下角的坐标x,坐标y,颜色资源,所使用的字体路径,所要显示的字符串);

<?php
//中文验证码
$char=array('你','我','他','好','啊','谁','觉','得','呢');
shuffle($char);//引用传参
$code=implode('',array_slice($char,0,4));

//画布
$im=imagecreatetruecolor(70,25);

//颜料
$gray=imagecolorallocate($im, 200, 200, 200);
$blue=imagecolorallocate($im, 0, 0, 255);

//填充
imagefill($im,0,0,$gray);

//写字
imagettftext($im,12,0,2,20,$blue,'./msyh.ttf',$code);

//输出
header('content-type:image/jpeg');
imagejpeg($im);

//销毁
imagedestroy($im);
?>
原文地址:https://www.cnblogs.com/lzzhuany/p/4784255.html