利用PHP GD库轻松制作php中文验证码教程

大家对php英文验证码数字验证码都很了解 大家手里一定有自己的一个php验证码在用  那么php中文验证码就很少人用 尤其对初学者来说 他们所看到的php视频教程都是非php中文验证码教程 其实php中文验证码是最简单的原理都一样都是利用php的GD库来制作的  下面就教你怎么利用php GD库制作php中文验证码
1、首先建立php验证码form表单

输入验证码:<input type=”text”/><img align=”absmiddle” src=”iiwnet.php” title=”看不清?点击更” alt=”看不清?点击更换” style=”cursor: pointer; margin-left: 0px;” onclick=”this.src=this.src+’?'” id=”vdimgck” original=”iiwnet.php”><br />
这样点击验证码图片即可更换验证码
2、新建一个文件,命名为:iiwnet.php,代码如下
<?php
Header(“Content-type: image/PNG”);
$ch_str=”这里是你要写验证码文字集合顶级大赛哦几个房间刻录软件付款即可平日男性乘客房贷放开思路房间里刷卡费率上浮临时搭建了交流时间的放开思路订飞机票群殴随便写越多越有空间概率”;//文字乱写即可写个三四百字最好
//上面的汉字UTF-8编码下是3个字节
$str=array();
for ($i=0;$i<strlen($ch_str);$i+=3)
{
$str[]=$ch_str[$i].$ch_str[$i+1].$ch_str[$i+2];
}
//图片的长和高
$image_x=200;
$image_y=100;
$im = imagecreate($image_x,$image_y);
//这里取图片底色为白色
$bkg = ImageColorAllocate($im,255,255,255);
//显示的字体样式,这个要把文件放到对应的目录中,如果你没有文件就去window的字体文件中找一个吧.
$fnt = “STXINGKA.TTF”;
//为图像分配一些颜色
$white=ImageColorAllocate($im,234,185,95);
//在图片上画椭圆弧,指定下坐标点
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
//在图片上画一条线段,指定下坐标点
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
//乱点的数量
$noise_num=3000;
$line_num=80;
//各种混乱字符的颜色
$rectangle_color=imagecolorallocate($im,0xAA,0xAA,0xAA);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
{
//在一个坐标点上画一个单一像素,这个点上面定义了,是黑色的.
imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
}
for($i=0;$i<$line_num;$i++)
{
$line_color=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//在两个坐标点间画一条线,颜色在上面定义
imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
}
$randnum=rand(0,count($str)-4);
//保持是偶数
if ($randnum%2)
{
$randnum+=1;
}
$str1=$str[$randnum].$str[$randnum+1].$str[$randnum+2].$str[$randnum+3];
for ($i=0;$i<4;$i++)
{
ImageTTFText($im, rand(28,32), rand(0,70), rand(($image_x/4)*$i+$image_x/10,($image_x/4)*$i+$image_x/8), rand($image_y/2+$image_y/10,$image_y/2+$image_y/5), $font_color, $fnt, $str[$randnum+$i]);
}
ImagePNG($im);
ImageDestroy($im);
?>
3、在iiwnet.php文件同级目录放入一个系统字库,重命名为:STXINGKA.TTF
其实php GD库很简单 也许新手看到这里面很多php代码都不认识 不要紧我现在也不记得 这东西你记住最好 不记住也没关系 你只要有php手册就行里面GD库函数都有讲解都有实例 大家照着写
创建资源  填充 画画  释放资源

订阅评论
提醒
guest的头像

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x