PHP產生驗證碼

來源:互聯網
上載者:User

標籤:height   create   nop   fill   fonts   type   ima   png   images   

<?php /** * php產生驗證碼 * @param $width 畫布寬 * @param $height 畫布高 * @param $vcodelen 驗證碼長度 * @param $pointnum 幹擾像素點數量 * @param $linenum 幹擾線條數量 * * 思路:建立驗證碼畫布,產生並填充背景色,產生驗證碼內容/幹擾像素點/線,填充到畫布,輸出。 */    $width =  100;    $height = 30;    $vcodelen = 4;    $pointnum = 200;    $linenum = 3;    // 建立畫布    $image = imagecreatetruecolor($width, $height);    // 建立色塊    $bgcolor = imagecolorallocate($image, 255, 255, 255);    // 填充畫布背景色    imagefill($image, 0, 0, $bgcolor);    // 驗證碼內容    for ($i=0; $i < $vcodelen; $i++) {         // 字型大小        $fontsize = 5;        // 字型顏色,顏色在限定範圍內隨機        $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));        $data = ‘abcdefghijklmnopqrstuvwxyz0123456789‘;        // 驗證碼內容在以上字串內隨機截取        $fontcontent = substr($data, rand(0,strlen($data)),1);        // 字串顯示位置        $x = ($i*$width/4)+rand(5,15);        $y = rand(5,10);        // 字串填充圖片        // imagestring的字型大小可選1-5,字型再大需要用imagettftext函數(需要字型檔)        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);        // imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, ‘/font/Geneva.dfont‘, $fontcontent);    }    // 幹擾像素點    for ($i=0; $i < $pointnum; $i++) {         $pointcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));        // 畫布填充像素點函數        imagesetpixel($image, rand(0,$width), rand(0,$height), $pointcolor);    }    // 幹擾線條    for ($i=0; $i < $linenum; $i++) {         $linecolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));        // 畫布填充線條函數        imageline($image, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $linecolor);    }    // 圖片輸出格式    header(‘content-type: image/png‘);    // 輸出驗證碼圖片    imagepng($image);    // 銷毀畫布    imagedestroy($image);?>

 

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.