中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

php驗證碼類

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
<?
 /**
  *   驗證碼類
  *   @author  firerat
  *   @email    lukai_rat@163.com
  *   @time     Feb 16  2011 15:28
  *   @lastmodify Feb 16  2011 15:28
  */

class verifycode{
        //image source
        private  $im;
        //圖片寬度
        private  $w;
        //圖片高度
        private  $h;
        //驗證字符集合
        private  $text;
        //字符數(shù)
        private $length;
        //字體
        private  $font = array();
        //字體大小
        private  $fontsize = array();
        //字體顏色
        private  $fontcolor = array();
        //字體的偏轉角度
        private  $angel = array();
        //背景色
        private  $backgroundcolor = array();
        //背景圖片
        private $backgroundimage = array();
        //坐標-x
        private $x  ;
        //坐標-y
        private $y  ;
        //驗證碼存放的字段
        private $checkcodefield ;

        /**
         * 構造函數(shù)
         *
         */
        public function __construct(){
          //圖片寬度
          $this->w = 500 ;
          //圖片高度
          $this->h = 600;
          //驗證字符
          $this->text = '0123456789qwertyuiopasdfghjklzxcvbnm';
          //字體
          $this->font = array(
                 'C:\\WINDOWS\\Fonts\\Ming Imperial.TTF',
                 'C:\\WINDOWS\\Fonts\\latha.TTF',
                 'C:\\WINDOWS\\Fonts\\ARIALNBI.TTF',
                 'C:\\WINDOWS\\Fonts\\GOTHICBI.TTF'
                );
          //字符數(shù)
          $this->length = '4';
          //字體大小
          $this->fontsize = array(50,60,70);
          //字體顏色
          $this->fontcolor = array(
                 array('red'=>0x14, 'green'=>0x37,'blue'=>0xad),
                 array('red'=>0x6e, 'green'=>0x86,'blue'=>0xd6),
                 array('red'=>0x2c, 'green'=>0x40 ,'blue'=>0x81),
                 array('red'=>0x06, 'green'=>0x1f ,'blue'=>0x70),
                 array('red'=>0x14, 'green'=>0x37 ,'blue'=>0xad),
                 array('red'=>0x57, 'green'=>0x79 ,'blue'=>0xc0)
          );
          //字體的偏轉角度
          $this->angel = array(2,4,8,-2,-4,-8);
          //背景色
          $this->backgroundcolor = array(
                  array('red'=>0x14, 'green'=>0x37,'blue'=>0xad),
                  array('red'=>0x6e, 'green'=>0x86,'blue'=>0xd6),
                  array('red'=>0x2c, 'green'=>0x40 ,'blue'=>0x81),
                  array('red'=>0x06, 'green'=>0x1f ,'blue'=>0x70),
                  array('red'=>0x14, 'green'=>0x37 ,'blue'=>0xad),
                  array('red'=>0x57, 'green'=>0x79 ,'blue'=>0xc0)
          );

          //背景圖片
          $this->backgroundimage = array(
                 'F:\\city_photo\\city_photo\\1\\1807141.jpg',
                 'F:\\city_photo\\city_photo\\1\\1807141.jpg',
                 'F:\\city_photo\\city_photo\\1\\1807141.jpg',
                 'F:\\city_photo\\city_photo\\1\\1807141.jpg',
                 'F:\\city_photo\\city_photo\\1\\1807141.jpg'
          );
          //坐標軸X
          $this->x = 100 ;
          //坐標軸Y
          $this->y = 300 ;
          //驗證碼存放的字段
          $this->checkcodefield = 'checkcode';
        }

        /* 創(chuàng)建背景
         *
         * @return $im
         */
        public function createImgae(){
           //$this->im = imagecreatetruecolor($this->w,$this->h);
           $key = array_rand($this->backgroundimage);
           $this->im = imagecreatefromjpeg( $this->backgroundimage[$key]);

           return $this->im;
        }

        /* 創(chuàng)建背景的索引色
         *
         *  @return
         */
        public function createBackgroundColor(){
           //獲取背景隨機顏色組key
           $rgbGroupKey = array_rand($this->backgroundcolor);
           $red = $this->backgroundcolor[$rgbGroupKey]['red'];
           $green = $this->backgroundcolor[$rgbGroupKey]['green'];
           $blue = $this->backgroundcolor[$rgbGroupKey]['blue'];

           //返回顏色索引
           return imagecolorallocate($this->im, $red, $green, $blue);
        }

        /* 獲取隨機字符,并將字符存放發(fā)到
         *
         *  @return
         */
        public  function getRandStr(){
           $randChars  = '';
           for($i = 0 ; $i < $this->length ; $i++){
               $randChars .= $this->text[rand(0,strlen($this->text))] ;
           }

           //字體編碼統(tǒng)一轉換為utf-8
           //$randChars = iconv("GB2312","UTF-8",$randChars);

           //寫進session
           $_SESSION[$this->checkcodefield] =  $randChars;

           return  $randChars;
        }

        /* 創(chuàng)建字體顏色索引
         * @return
         */
        public function createFontColor(){
           //獲取背景隨機顏色組key
           $rgbGroupKey = array_rand($this->fontcolor);
           $red = $this->fontcolor[$rgbGroupKey]['red'];
           $green = $this->fontcolor[$rgbGroupKey]['green'];
           $blue = $this->fontcolor[$rgbGroupKey]['blue'];

           //顏色索引
           return  imagecolorallocate($this->im, $red, $green, $blue);;
        }

        //添加文字到圖片
        public function addTextToImage(){
          //字體顏色
          $fontcolor =$this->createFontColor();
          //字體
          $key = array_rand($this->font);
          $font = $this->font[$key];

          //驗證碼
          $text = $this->getRandStr();

          //偏轉角度
          $key = array_rand($this->angel);
          $_angle = $this->angel[$key];

          //起始坐標
          $x = $this->x;
          $y = $this->y;

          //字體大小
          $key = array_rand($this->fontsize);
          $_fontsize = $this->fontsize[$key];

          //添加文字到圖片
          imagettftext($this->im, $_fontsize , $_angle, $x, $y, $fontcolor, $font, $text);
        }

        /**
         * 輸出圖片
         *
         */
        public function outputImage(){
          //創(chuàng)建布景
          $this->createImgae();
          //顏色添加到布景
          $this->createBackgroundColor();
          //添加文字到圖片
          $this->addTextToImage();

          //增加過期,30分鐘后過期
          $expireTime = date("M, d Y H:i:s",time()+1800);
          header("Expires: {$expireTime} GMT ");
          header("Last-Modified: " . gmdate("D, d M Y H:i:s")." GMT");
          header("Cache-Control: no-store, no-cache, must-revalidate");
          header("Cache-Control: post-check=0, pre-check=0", false);
          header("Pragma: no-cache");

          //聲明輸出文件的類型
          header('Content-type: image/png');
          //輸出png圖片
          imagepng($this->im);
          //釋放與此圖片關聯(lián)的內(nèi)存
          imagedestroy($this->im);
        }

    }

    $instance = new verifycode;
    $instance->outputImage();

驗證碼效果圖:

php驗證碼效果圖


標簽:

版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Python實現(xiàn) 深度優(yōu)先算法生成迷宮

下一篇:獲取某個月的最后一天或某個月的天數(shù)