搜尋
首頁php教程php手册php學習筆記:利用gd庫產生圖片,並實作隨機驗證碼

說明:一些基本的程式碼我都做了註釋,這裡實作的驗證碼位數、需要用的字串都可以再設定。有我的註釋,大家應該很容易看得懂。

基本思路:

     1.用mt_rand()隨機產生數字決定需要取得的字串,對字串進行拼接(覺得產生的驗證碼覺得有點太擠,大家可以再字串中間拼接個空格鍵),實現隨機驗證碼;

     備註:建議大家用mt_rand(),而不是rand(),前者效率更高

     2.利用gd庫產生圖片,將隨機字串寫到圖片輸出。

效果:

每次刷新,都會產生一個隨機驗證,後期我可能還會補充怎麼實現隨機碼點擊圖片就再次更新

 

代碼:

 

<span style="font-size: 18px;"><span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建画布</span>
<span style="color: #800080;">$width</span> = 120;   <span style="color: #008000;">//</span><span style="color: #008000;"> 规定画布的宽高</span>
<span style="color: #800080;">$height</span> = 45<span style="color: #000000;">;
</span><span style="color: #800080;">$image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 创建一幅真彩色图像
// 添加一些即将用到的颜色</span>
<span style="color: #800080;">$white</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xf2, 0xec, 0xe0<span style="color: #000000;">);
</span><span style="color: #800080;">$orange</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xff, 0xa5, 0x4c<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 对画布背景填充颜色</span>
imagefill(<span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$white</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">mt_rand  获取随机数 mt_rand(min, max);</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;">  str_rand(){
        </span><span style="color: #800080;">$str</span>="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ"<span style="color: #000000;">;
        </span><span style="color: #800080;">$rand</span>=""<span style="color: #000000;">;
        </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>$i</span>++){<span style="color: #008000;">//</span><span style="color: #008000;">获取5个随机的字符串</span>
            <span style="color: #800080;">$rand</span> .= <span style="color: #800080;">$str</span>[<span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$str</span>)-1)];  <span style="color: #008000;">//</span><span style="color: #008000;">如:随机数为30  则:$str[30]</span>
<span style="color: #000000;">        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$rand</span><span style="color: #000000;">;
    }
</span><span style="color: #800080;">$verifyCode</span>=<span style="color: #000000;">str_rand();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 画一串字符串在画布上</span>
imagestring(<span style="color: #800080;">$image</span>, 10, 10, 10, "<span style="color: #800080;">$verifyCode</span>", <span style="color: #800080;">$orange</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通知浏览器输出的是图像(png类型)</span>
<span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出到浏览器</span>
imagepng(<span style="color: #800080;">$image</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 释放图像资源</span>

 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境