php 驗證碼圖片 程式

來源:互聯網
上載者:User

<?php php 驗證碼圖片 程式
/*
*檔案名稱:class.safeCode.php
*類名:safeCode
*目的:產生web應用時所需的驗證碼圖片
*目前的版本:1.0.2
*作者:NoAngels
*連絡方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com
*開發時間:2008年06月20日
*最後更新:2008年06月21日
*更新內容:
*版本1.0.2
*1.更新了對linux主機支援,設定GDFONTPATH環境變數,
* putenv('GDFONTPATH=' . realpath('.'));
*2.對不支援antialias函數的時候避免採用此方法,避免低版本的GD庫
*版本1.0.1
*1.更新了繪製指定類型圖片,支援類型有:png,jpeg,gif.預設為png(此效果最佳)
*2.最佳化了部分代碼
*/
class safeCode{
        function __construct(){
                #設定預設safe安全保護碼
                $this->__safe = substr(md5(time()), 0 , 4);
        }
        function __destruct(){
                #釋放資源
                unset($this->__img, $this->__canvasWidth, $this->__canvasHeight, $this->__codeLength, $this->__spacePerChar, $this->__code, $this->__safe, $this->__background, $this->__borderColor, $this->__colors, $this->__font, $this->__sessionName);
        }
        function setCanvas($width = 200, $height = 60){
                #設定畫布寬度以及高度.有預設值
                $this->__canvasWidth = $width;
                $this->__canvasHeight = $height;
                $this->__img = imagecreatetruecolor($width, $height);               
                return;
        }
        function setSafe($char){
                #設定安全碼,不調用系統會自己設定
                $this->__safe = $char;
                return;
        }
        function setCodeLength($num = 8){
                #設定驗證碼長度
                $this->__codeLength = $num;
        }       
        function setFont($fontName){
                #設定繪圖時所用的字型,字型檔必須與類檔案同目錄
                $this->__font = $fontName;
                return;
        }
        function setBackground($arg1 = 255, $arg2 = 255, $arg3 = 255){
                #設定背景顏色
                $this->__background = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
                return;
        }
        function setBorderColor($arg1 = 128, $arg2 = 128, $arg3 = 128){
                #設定邊框顏色
                $this->__borderColor = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
        }
        function setFontColor($arr){
                #設定繪圖時所用顏色,參數為顏色集合數組
                foreach($arr as $color){
                        $this->__colors[] = imagecolorallocate($this->__img, $color[0], $color[1], $color[2]);
                }
                return;
        }       
        function setSession($sessionName = 'safeCode'){
                #設定session變數名,預設為$_SESSION['safeCode']
                $this->__sessionName = $sessionName;
                #$_SESSION[$sessionName] = $this->__code;
                return;
        }
        function display($type = 'png'){
                #顯示圖片,可指定顯示類型,目前支援png(預設),jpeg,gif
                $this->__setSpacePerChar();
                imagefilledrectangle($this->__img, 1, 1, $this->__canvasWidth - 2, $this->__canvasHeight -2, $this->__background);
                imagerectangle($this->__img, 0, 0, $this->__canvasWidth - 1, $this->__canvasHeight - 1, $this->__borderColor);
                $this->__drawText($this->__colors, $this->__getCode(), $this->__font);
                #修正linux以及低版本的GD庫問題
                if(function_exists(imageantialias)){
                   imageantialias($this->__img, true);
                }
                $_SESSION[$this->__sessionName] = $this->__code;
                switch ($type){
                        case 'png':
                                header('Content-type:image/png');                               
                                imagepng($this->__img);       
                        case 'jpeg':
                                header('Content-type:image/jpeg');                               
                                imagejpeg($this->__img, '' , 75);
                        case 'gif':
                                header('Content-type:image/gif');                               
                                imagegif($this->__img);       
                        default:
                                header('Content-type:image/png');                               
                                imagepng($this->__img);                                                                                                               
                }
                imagegif($this->__img);       
        }       
        private function __setSpacePerChar(){
                #私人函數,設定字元間隔
                $this->__spacePerChar = $this->__canvasWidth / $this->__codeLength;
                return;
        }
        private function __getCode(){
                #擷取驗證碼
                $this->__code = substr(md5(time().$this->__safe), rand(0, 24), $this->__codeLength);
                return $this->__code;
        }
        private function  __drawText($colors, $code, $fontName){
                #開始繪圖
                #設定GBFONTPATH為目前的目錄,不然linux環境下會報錯
                putenv('GDFONTPATH=' . realpath('.'));
                for($i = 0; $i < strlen($code); $i++){
                        $color = $colors[$i % count($colors)];
                        imagettftext(
                                $this->__img,
                                24 + rand(0, 8),
                                -20 + rand(0, 40),
                                ($i + 0.3) * $this->__spacePerChar,
                                $this->__canvasHeight - 20 + rand(0, 20),
                                $color,
                                $fontName,
                                $code{$i}
                        );
                }
                #繪製隨機實線
                for($i = 0; $i < 400; $i++){
                        $x1 = rand(5, $this->__canvasWidth - 5);
                        $y1 = rand(5, $this->__canvasHeight - 5);
                        $x2 = $x1 - 4 + rand(0, 8);
                        $y2 = $y1 - 4 + rand(0, 8);
                        imageline($this->__img, $x1, $y1, $x2, $y2, $this->__colors[rand(0, count($this->__colors) - 1)]);
                }
                return;
        }
        private $__img = NULL;
        private $__canvasWidth = 120;
        private $__canvasHeight = 80;
        private $__codeLength = 8;
        private $__spacePerChar = 0;
        private $__code = 12345678;
        private $__safe = 2008;
        private $__background = NULL;
        private $__borderColor = NULL;       
        private $__colors = array();
        private $__font = 'arial.ttf';
        private $__sessionName = '';
}
?>

使用方法.

<?php
#測試檔案
include_once('class.safeCode.php');
session_start();
$safeCode = new safeCode;
$safeCode->setCanvas(200, 50);
$safeCode->setCodeLength(8);
$safeCode->setSafe('2008');#設定安全碼,不設定有預設值,動態產生
$safeCode->setBackground();
$safeCode->setBorderColor();
$safeCode->setFont('arial.ttf');#設定字型.必須同目錄,字型檔
$colors = array(array(128, 64, 192), array(192, 64, 128), array(108, 192, 64));
$safeCode->setFontColor($colors);
$safeCode->setSession();
$safeCode->display('jpeg');

?>

相關文章

聯繫我們

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