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

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

WBOY
풀어 주다: 2016-06-13 11:30:45
원래의
1724명이 탐색했습니다.

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>?>
로그인 후 복사

<?<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>
로그인 후 복사

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿