產生驗證碼的原理很簡單,一個字’畫’.沒錯,驗證碼我們要畫的有背景,數字或字母。
效果如圖:
步驟如下:
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><<span>400</span>;<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><<span>5</span>;<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><<span>$num</span>;<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><<span>$m</span>;<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教學有興趣的朋友有幫助。