您的位置:首页 > 编程语言 > PHP开发

php图片验证码的制作(高级篇)

2009-02-25 14:18 477 查看
图片验证码的制作(下)
学习目标:
1.image与header输出的介绍
2.imageline与imagesetpixel函数(设置图片的干扰线和噪点)
3.imagettftext函数调用字体写入文字(系统使用的字体调用到PHP)
4.php验证码插入中文的方法

1.image与header输出的介绍
header定义头的动作,php5中支持三种类型
Content-type:xxx/yyy 文件的类型 如:image/jpeg image/gif image/png
Location:url 自动跳转页面
Status:nnn xxx 状态

GD库中对应的image类型
imagejpeg(*) imagegif(*)...

2.imageline与imagepixel函数
imageline画线函数
imageline(resource image,int x1,int y1,int x2,int y2,int color)

imagepixel画点函数
imagepixel(resource image,int x,int y,int color)

3.imagettftext函数
imagettftext带字体的写入函数
imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text)

4.php验证码插入中文的方法
iconv('gb2312','utf-8','编码转换')//将gb2312转换成utf-8格式

check2.php

<?php
session_start();
for($i=0;$i<4;$i++)
{
//$shui=rand(0,15);
//echo dechex($shui);//dechex函数十六进制可以把十进制转换成字母
$shui.=dechex(rand(1,15));
}

$im=imagecreatetruecolor(100,30);//创建一个真彩色图片,宽100px,高30px

//创建颜色
$bg=imagecolorallocate($im,0,0,0);//第一次用调试板时为背景颜色,000为黑色
$te=imagecolorallocate($im,255,255,255);//255,255,255为白色

//画线条(干扰线)
for($i=0;$i<4;$i++)
{
$te2=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//255,255,255为白色
imageline($im,0,rand(0,20),100,rand(0,30),$te2);
}

//画点(噪点)
for($i=0;$i<200;$i++)
{
imagesetpixel($im,rand(0,100),rand(0,30),$te2);
}

//写入中文
$str=iconv('gb2312','utf-8','写入中文');//编码转换
imagettftext($im,12,5,20,20,$te,'simkai.ttf',$str);
$_SESSION[check_pic]=$str;
//输出图片
header('Content-type:image/jpeg');
imagejpeg($im);
?>


sub2.php

<?php
session_start();
$str=$_SESSION['check_pic'];
$str=iconv('utf-8','gb2312',$str);//把编码格式为utf-8的session转换为编码格式为gb2312

if($_POST['check'])
{
if($_POST['check']==$str)
{
echo '验证码正确:',$str;
}
else{echo '验证码错误:',$str;}
}
?>
<form action="" method="post">
<img src='check2.php'><br/>
<input type="text" name="check" maxlength="4">
<input type="submit" value="提交">
</form>


查看示例
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: