Home > php教程 > php手册 > PHP5 GD库生成图形验证码(汉字)

PHP5 GD库生成图形验证码(汉字)

WBOY
Release: 2016-06-13 11:30:45
Original
1716 people have browsed it

1,利用GD库函数生成图片,并在图片上写指定字符
imagecreatetruecolor 新建一个真彩色图像
imagecolorallocate 为一幅图像分配颜色(调色板)
imagestring 绘制字符
imageline 绘制线条
imagesetpixel 打像素点
2,输出图片
imagejpeg($img);
PHP实现过程,代码中注释详细,这里不做过多解释
verify.php

<?<span php
</span><span //</span><span 1.qi启用gd库GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。
// 在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。
//by www.jbxue.com 2013-7-30</span>
<span session_start</span><span ();
</span><span //</span><span  把GBK编码的字符串转换成UTF-8字符串,第一个参数之所以写GBK,是因为本php文件在主机中存储的编码是GBK编码
// UTF-8编码浏览器普遍支持,通用性强,这里就转换成UTF-8</span>
<span $str</span> = <span iconv</span>("GBK", "utf-8", "芸芸众生绿水青山名胜古迹敞开心胸便会云蒸霞蔚快乐将永远伴随着你"<span );
</span><span if</span>(!<span is_string</span>(<span $str</span>) || !mb_check_encoding(<span $str</span>,"utf-8"<span ))
{
</span><span exit</span>("不是字符串或者不是utf-8"<span );
}
</span><span $zhongwenku_size</span><span ;
</span><span //</span><span  按UTF-8编码方式获取字符串的长度</span>
<span $zhongwenku_size</span> = mb_strlen(<span $str</span>,"UTF-8"<span );
</span><span //</span><span  把上述字符导入数组中</span>
<span $zhongwenku</span> = <span array</span><span ();
</span><span for</span>( <span $i</span>=0; <span $i</span><<span $zhongwenku_size</span>; <span $i</span>++<span )
{
</span><span $zhongwenku</span>[<span $i</span>] = mb_substr(<span $str</span>, <span $i</span>,1,"UTF-8"<span );
}
</span><span $result</span> = ""<span ;
</span><span //</span><span  图片上要写入的四个字符</span>
<span for</span>(<span $i</span>=0; <span $i</span><4; <span $i</span>++<span )
{
</span><span switch</span> (<span rand</span>(0, 1<span ))
{
</span><span case</span> 0:
<span $result</span>.=<span $zhongwenku</span>[<span rand</span>(0, <span $zhongwenku_size</span>-1<span )];
</span><span break</span><span ;
</span><span case</span> 1:
<span $result</span>.=<span dechex</span>(<span rand</span>(0,15<span ));
</span><span break</span><span ;
}

}
</span><span $_SESSION</span>["check"] = <span $result</span><span ;

</span><span //</span><span  创建一个真彩图片 宽100,高30</span>
<span $img</span> = imagecreatetruecolor(100, 30<span );
</span><span //</span><span  分配背景颜色</span>
<span $bg</span> = imagecolorallocate(<span $img</span>, 0, 0, 0<span );
</span><span //</span><span  分配文字颜色</span>
<span $te</span> = imagecolorallocate(<span $img</span>, 255,255,255<span );
</span><span //</span><span  在图片上写字符串
//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);
// 在图片上根据载入字体可以写出特殊字体</span>
imagettftext(<span $img</span>, 13, <span rand</span>(2, 9), 20 ,20, <span $te</span>, "MSYH.TTF",<span $result</span><span );
</span><span $_SESSION</span>["check"] = <span $result</span><span ;
</span><span for</span>(<span $i</span>=0; <span $i</span><3; <span $i</span>++<span )
{
</span><span //</span><span  $t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));
// 画线</span>
imageline(<span $img</span>, 0, <span rand</span>(0, 20), <span rand</span>(70,100), <span rand</span>(0, 20), <span $te</span><span );
}
</span><span $t</span> = imagecolorallocate(<span $img</span>, <span rand</span>(0, 255),<span rand</span>(0, 255),<span rand</span>(0, 255<span ));
</span><span //</span><span  为图片添加噪点</span>
<span for</span>(<span $i</span>=0; <span $i</span><200; <span $i</span>++<span )
{
imagesetpixel(</span><span $img</span>, <span rand</span>(1, 100), <span rand</span>(1, 30), <span $t</span><span );
}
</span><span //</span><span  发送http头信息 指定本次发送的是image中的jpeg</span>
<span header</span>("Content-type: image/jpeg"<span );
</span><span //</span><span  输出jpeg图片至浏览器</span>
imagejpeg(<span $img</span><span );
</span>?>
Copy after login

<?<span php
</span><span //</span><span 提交页面
//by www.jbxue.com</span>
<span session_start</span><span ();
</span><span if</span>(@<span $_POST</span>['check'<span ])
{
</span><span if</span>(<span $_POST</span>["check"] == <span $_SESSION</span>["check"<span ])
{
</span><span echo</span>" 恭喜你!验证码输入正确!"<span ;
}</span><span else</span><span {
</span><span echo</span>" 不好意思验证码输入错误"<span ;
}
}
</span>?>
<form action="" method="post">
<img  src="verify.php"/ alt="PHP5 GD库生成图形验证码(汉字)" > </br>
<input type="text" name="check"></br>
<input type="submit" value="submit"></br>
</form>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template