php學習筆記:利用gd庫產生圖片,並實現隨機驗證碼

來源:互聯網
上載者:User

標籤:

說明:一些基本的代碼我都進行了注釋,這裡實現的驗證碼位元、需要用的字串都可以再設定。有我的注釋,大家應該很容易能看得懂。

基本思路:

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

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

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

效果:

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

 

代碼:

 

<?php// 建立畫布$width = 120;   // 規定畫布的寬高$height = 45;$image = imagecreatetruecolor($width, $height);  // 建立一幅真彩色映像// 添加一些即將用到的顏色$white = imagecolorallocate($image, 0xf2, 0xec, 0xe0);$orange = imagecolorallocate($image, 0xff, 0xa5, 0x4c);// 對畫布背景填充顏色imagefill($image, 0, 0, $white);//mt_rand  擷取隨機數 mt_rand(min, max);    function  str_rand(){        $str="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ";        $rand="";        for($i=0; $i<5; $i++){//擷取5個隨機的字串            $rand .= $str[mt_rand(0, strlen($str)-1)];  //如:隨機數為30  則:$str[30]        }        return $rand;    }$verifyCode=str_rand();// 畫一串字串在畫布上imagestring($image, 10, 10, 10, "$verifyCode", $orange);// 通知瀏覽器輸出的是映像(png類型)header(‘Content-Type: image/png‘);// 輸出到瀏覽器imagepng($image);// 釋放映像資源

 

php學習筆記:利用gd庫產生圖片,並實現隨機驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.