PHP如何產生驗證碼

來源:互聯網
上載者:User
產生驗證碼的原理很簡單,一個字’畫’.沒錯,驗證碼我們要畫的有背景,數字或字母。

效果:

步驟如下:
1.擷取隨機驗證碼
用getCode函數(自訂),它會返回一個字串.

2.建立一個映像資源、分配顏色

$m = imagecreatetruecolor($width,$height);

imagecolorallocate,這個其實就是擷取一種顏色

3.開始繪畫
1).在image映像左上方處開始地區填充背景顏色

imagefill($m,0,0,$bg);

2).添加一個有顏色的矩形框
imagerectangle

3).添加幹擾點和幹擾線

4).把擷取到的驗證碼字串畫像上去

4.輸出映像
1).直接輸出到瀏覽器
//注意此函數執行前不能有輸出,空格也不行
//如果沒有設定回應標頭,則頁面會出現亂碼,而不是一張驗證碼的映像
header(“Content-type:image/png”); //設定回應標頭資訊
imagepng($m);

2).輸出到檔案中

imagepng($m,'test.png');

5.銷毀映像
imagedestroy($m);

代碼如下:

/** * @param int $num  驗證碼的個數,預設為4 * @param int $type 驗證碼的類型,0:純數字,1:數字+小寫字母 2:數字+大小寫字母 * @param bool $outFile 驗證碼是否輸出到檔案中 * @return array 以數組形式返回驗證碼和驗證碼圖片名字 */function drawIdentifyCode($num=4,$type=0,$outFile = true){    //繪製驗證碼$code = getCode($num,$type);//擷取驗證碼$width = $num*35;    $height = 40;//1.建立一個畫映像資源、分配顏色$m = imagecreatetruecolor($width,$height);    $c = array(imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255)),        imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255)),        imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255)),        imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255)));    $bg = imagecolorallocate($m,220,220,220); //背景顏色//2.開始繪畫//在image映像左上方處開始地區填充    imagefill($m,0,0,$bg);//添加一個有顏色的矩形框    imagerectangle($m,0,0,$width-1,39,$c[0]);//添加幹擾點for($i=0;$i<400;$i++)        imagesetpixel($m,rand(0,$width),rand(0,30),$c[$i%4]);//添加幹擾線for($i=0;$i<5;$i++)        imageline($m,rand(0,$width),rand(0,30),rand(0,$width),rand(0,30),$c[$i%4]);//繪製驗證碼內容(一個一個字元繪製)for($i=0;$i<$num;$i++)        imagettftext($m,28,rand(-50,50),15+(28*$i),30,$c[$i%4],"consola.ttf",$code[$i]);//3.輸出映像$fileName = null;    if(!$outFile)    {        //注意此函數執行前不能有輸出,空格也不行//如果沒有設定回應標頭,則頁面會出現亂碼,而不是一張驗證碼的映像        header("Content-type:image/png"); //設定回應標頭資訊        imagepng($m);    }    else    {        $fileName = time().'.png';        imagepng($m,$fileName);    }//4.銷毀圖片    imagedestroy($m);    return array($code,$fileName);}/** * @function 隨機產生一個驗證碼的函數 * @param $m: 驗證碼的個數(預設為4) * @param $type:驗證碼的類型:0:純數字,1:數字+小寫字母 2:數字+大小寫字母 * @return 返回字串形式的驗證碼  */function getCode($m=4,$type=0){    $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";    $t = array(9,35,strlen($str)-1); //驗證碼類型//隨機產生驗證碼所需內容$c = "";    for($i=0;$i<$m;$i++)        $c.=$str[rand(0,$t[$type])];    return$c;}

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介紹了 PHP如何產生驗證碼,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.