function create_visivvle_code($length)
{
$rand_code=array('说','特','也','名','豪','你','要','前','女','虫','仙','乱','得','糗','念','练','年','日','天','古','月','的','云','想','一','发','意','孤','行','相','的','时','厚');
header("content-type: image/png");
$image_x=$length*30; //图片宽度
$image_y=40; //图片高度
$noise_num=80*$length; //杂点数量
$line_num=$length-2; //干扰线数量
$image=imagecreate($image_x,$image_y);
imagecolorallocate($image,0xff,0xff,0xff); //设定背景颜色
$rectangle_color=imagecolorallocate($image,0xAA,0xAA,0xAA); //边框颜色
$noise_color=imagecolorallocate($image,0x00,0x00,0x00); //杂点颜色
$font_color=imagecolorallocate($image,0xAA,0x00,0x00); //字体颜色
$line_color=imagecolorallocate($image,0x33,0x33,0x33); //干扰线颜色
//加入杂点
for($i=0;$i<$noise_num;$i++)
{
imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
}
$font_face="simfang.ttf"; //字体
$x=2;
$session_code='';
for($i=0;$i<$length;$i++)
{
$code=$rand_code[mt_rand(0,count($rand_code)-1)];
imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,$code);
$x+=30;
$session_code.=$code;
}
@session_start();
$_SESSION['excode']=$session_code; //把附加码的值放在session中
//加入干扰线
for($i=0;$i<$line_num;$i++)
{
imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color); //加个边框
imagepng($image);
imagedestroy($image);
}
}
直接调用方法 create_visible_code();
create_visivvle_code(4); //--> 4 : 为需要显示的验证码长度
注意:① 方法中 simfang.ttf文件 为字体文件(必须要支持中文 ),可以根据自己需要改变。存在 安装系统的盘下:
以我的为例 C:/WINDOWS/Fonts
② 该方法目前只测试过UTF-8 格式的字符。
生成中文验证码的PHP实现
577

被折叠的 条评论
为什么被折叠?



