php中图片验证码的制作,php5中文图片验证码的制作方法

在php中,生成中文图片格式的验证码,步骤如下:

1、image与header输出的介绍

2、imageline 与 imagesetpixel 函数  这是验证码中的噪点

3、imagettftext函数调用字体写入文字

4、PHP验证码插入中文的方法

参考之前的php 中文验证码的实现方法,实现了如下的中文图片验证码的雏形:

复制代码 代码示例:

//中文图片验证码

session_start();

for($i=0;$i<4;$i++){

$rand.=dechex(rand(1,15));

}

$_SESSION[check_pic]=$rand;

$im=imagecreatetruecolor(100,30);

//设置颜色

$bg = imagecolorallocate($im,0,0,0);//第一次用时的背景颜色

$te = imagecolorallocate($im,255,255,255);//后面用的颜色

//划线条

for($i=0;$i<3;$i++);{

$te2 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imageline($im,rand(0,100),0,100,30,$te2);

}

//把字符串写在图像左上角

imagestring($im,rand(3,6),rand(1,70),rand(1,10),$rand,$te);

//输出图像

header("Content-type: image/jpeg");

imagejpeg($im);

?>

修改后的最终版本:

复制代码 代码示例:

//中文图片验证码

session_start();

for($i=0;$i<4;$i++){

$rand.=dechex(rand(1,15));

}

$_SESSION[check_pic]=$rand;

$im=imagecreatetruecolor(100,30);

//设置颜色

$bg = imagecolorallocate($im,0,0,0);//第一次用时的背景颜色

$te = imagecolorallocate($im,255,255,255);//后面用的颜色

//划线条

for($i=0;$i<3;$i++){

$te2 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imageline($im,rand(0,100),0,100,30,$te2);

} // www.jbxue.com

//点

for($i=0;$i<200;$i++){

imagesetpixel($im,rand()%100,rand()%30,$te2);

}

//转换为utf8,确定要绘制的中午文字

$str = iconv("GBK","UTF-8","新年快乐");

//载入字体

imagettftext($im,12,9,20,20,$te,'FZSTK.TTF',$str);

//把字符串写在图像左上角

//imagestring($im,rand(1,6),rand(3,70),rand(0,16),$rand,$te);

//输出图像

header("Content-type: image/jpeg");

imagejpeg($im);

?>

当然,这只是我的习作而已,更多php验证码的例子,大家可以参考php 验证码类以及php 图片验证码中的实现方法。

看看高手们是如何实现php验证码的。脚本小编祝大家学习进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值