生成验证码的原理很简单,一个字’画’.没错,验证码我们要画的有背景,数字或字母。
效果如图:
步骤如下:
1.获取随机验证码
用getCode函数(自定义),它会返回一个字符串.
2.创建一个图像资源、分配颜色
<code><span>$m</span> = imagecreatetruecolor(<span>$width</span>,<span>$height</span>);</code>
imagecolorallocate,这个其实就是获取一种颜色
3.开始绘画
1).在image图像左上角处开始区域填充背景颜色
<code>imagefill(<span>$m</span>,<span>0</span>,<span>0</span>,<span>$bg</span>);</code>
2).添加一个有颜色的矩形框
imagerectangle
3).添加干扰点和干扰线
4).把获取到的验证码字符串画上图像上去
4.输出图像
1).直接输出到浏览器
//注意此函数执行前不能有输出,空格也不行
//如果没有设置响应头,则页面会出现乱码,而不是一张验证码的图像
header(“Content-type:image/png”); //设置响应头信息
imagepng($m);
2).输出到文件中
<code>imagepng(<span>$m</span>,<span>'test.png'</span>); </code>
5.销毁图像
imagedestroy($m);
代码如下:
<code><span>/** * @param int $num 验证码的个数,默认为4 * @param int $type 验证码的类型,0:纯数字,1:数字+小写字母 2:数字+大小写字母 * @param bool $outFile 验证码是否输出到文件中 * @return array 以数组形式返回验证码和验证码图片名字 */</span> function drawIdentifyCode(<span>$num</span>=<span>4</span>,<span>$type</span>=<span>0</span>,<span>$outFile</span> = true) { <span>//绘制验证码</span><span>$code</span> = getCode(<span>$num</span>,<span>$type</span>);<span>//获取验证码</span><span>$width</span> = <span>$num</span><span>*35</span>; <span>$height</span> = <span>40</span>; <span>//1.创建一个画图像资源、分配颜色</span><span>$m</span> = imagecreatetruecolor(<span>$width</span>,<span>$height</span>); <span>$c</span> = array(imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)), imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)), imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)), imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>))); <span>$bg</span> = imagecolorallocate(<span>$m</span>,<span>220</span>,<span>220</span>,<span>220</span>); <span>//背景颜色</span><span>//2.开始绘画</span><span>//在image图像左上角处开始区域填充</span> imagefill(<span>$m</span>,<span>0</span>,<span>0</span>,<span>$bg</span>); <span>//添加一个有颜色的矩形框</span> imagerectangle(<span>$m</span>,<span>0</span>,<span>0</span>,<span>$width</span>-<span>1</span>,<span>39</span>,<span>$c</span>[<span>0</span>]); <span>//添加干扰点</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>400;<span>$i</span>++) imagesetpixel(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>$width</span>),<span>rand</span>(<span>0</span>,<span>30</span>),<span>$c</span>[<span>$i</span><span>%4</span>]); <span>//添加干扰线</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>5;<span>$i</span>++) imageline(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>$width</span>),<span>rand</span>(<span>0</span>,<span>30</span>),<span>rand</span>(<span>0</span>,<span>$width</span>),<span>rand</span>(<span>0</span>,<span>30</span>),<span>$c</span>[<span>$i</span><span>%4</span>]); <span>//绘制验证码内容(一个一个字符绘制)</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$num;<span>$i</span>++) imagettftext(<span>$m</span>,<span>28</span>,<span>rand</span>(-<span>50</span>,<span>50</span>),<span>15</span>+(<span>28</span><span>*$</span>i),<span>30</span>,<span>$c</span>[<span>$i</span><span>%4</span>],<span>"consola.ttf"</span>,<span>$code</span>[<span>$i</span>]); <span>//3.输出图像</span><span>$fileName</span> = null; <span>if</span>(!<span>$outFile</span>) { <span>//注意此函数执行前不能有输出,空格也不行</span><span>//如果没有设置响应头,则页面会出现乱码,而不是一张验证码的图像</span> header(<span>"Content-type:image/png"</span>); <span>//设置响应头信息</span> imagepng(<span>$m</span>); } <span>else</span> { <span>$fileName</span> = time().<span>'.png'</span>; imagepng(<span>$m</span>,<span>$fileName</span>); } <span>//4.销毁图片</span> imagedestroy(<span>$m</span>); <span>return</span> array(<span>$code</span>,<span>$fileName</span>); } <span>/** * @function 随机生成一个验证码的函数 * @param $m: 验证码的个数(默认为4) * @param $type:验证码的类型:0:纯数字,1:数字+小写字母 2:数字+大小写字母 * @return 返回字符串形式的验证码 */</span> function getCode(<span>$m</span>=<span>4</span>,<span>$type</span>=<span>0</span>) { <span>$str</span> = <span>"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>; <span>$t</span> = array(<span>9</span>,<span>35</span>,strlen(<span>$str</span>)-<span>1</span>); <span>//验证码类型</span><span>//随机生成验证码所需内容</span><span>$c</span> = <span>""</span>; <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$m;<span>$i</span>++) <span>$c</span>.=<span>$str</span>[<span>rand</span>(<span>0</span>,<span>$t</span>[<span>$type</span>])]; <span>return</span><span>$c</span>; }</code>
以上就介绍了 PHP如何生成验证码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。