PHP碎碼——自己寫的驗證碼

來源:互聯網
上載者:User

標籤:php


其實裡面沒必要封裝函數,只是當時覺得視覺上好看而已,結構清晰點


<?php       class captcha{    //驗證碼-字串    private $codes;    //圖片長度    private $img_length = 150;    //圖片高度    private $img_height = 30;    //字元列表,用以產生隨機驗證碼    private $charlist = ‘1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM‘;    //隨機碼的個數    private $code_num = 4;    //字型大小--初始化時算出的    private $font_size ;    //幹擾線數目    private $line_num = 5;    //幹擾雪花數目    private $sterisk_num = 50;    //驗證碼--圖片    private $img;    //字型檔路徑    private $ttf = ‘./instance/font/Elephant.ttf‘;        public function __construct(){                   //字型大小通過圖片寬高動態產生的,但感覺不太完美        $this->font_size = ($this->img_height*2/5 > $this->img_height*4/5 ? $this->img_height*4/5 : $this->img_height*2/5);    }        public function run(){        //建立圖片資源        $this->createImage();        //往圖片中添加雪花        $this->addaSterisk();        //往圖片中添加字元        $this->addfont();        //往圖片中添加線條        $this->addLine();        //將圖片輸出至瀏覽器        $this->outputImg();    }    //返回驗證碼字串    public function getCode(){        return $this->codes;    }        //建立圖片資源    private function createImage(){        //建立圖片資源        $this->img = imagecreatetruecolor($this->img_length,$this->img_height);          //建立顏色        $color_bg = imagecolorallocate($this->img, mt_rand(210, 255), mt_rand(210, 255), mt_rand(210, 255));        //設定圖片背景色        imagefill($this->img, 0, 0, $color_bg);    }        //往圖片中添加線條    private function addLine(){        //添加指定數量的線條        for ($i = 0; $i < $this->line_num; $i++) {            //建立隨機顏色--參數(圖片資源,R,B,G)            $color_line = imagecolorallocate($this->img, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));                        //添加線條,位置隨機--參數(圖片資源,起點-x,起點-y,終點-x,終點-y,顏色)            //不可調整            //imageline($this->img, mt_rand(0, $this->img_length), mt_rand(0, $this->img_height), mt_rand(0, $this->img_length), mt_rand(0, $this->img_height), $color_line);            //可以調整線條的粗細            $src_x = mt_rand(0, $this->img_length);            $src_y = mt_rand(0, $this->img_height);            $dest_x = mt_rand(0, $this->img_length);            $dest_y = mt_rand(0, $this->img_height);            for ($j = 0; $j < 1; $j++) {                imageline($this->img, $src_x+$j, $src_y+$j, $dest_x+$j,$dest_y+$j, $color_line);            }        }    }    //往圖片中添加雪花    private function addaSterisk(){        //添加指定數量的雪花        for ($i = 0; $i < $this->sterisk_num; $i++) {            //建立隨機顏色--參數(圖片資源,R,B,G)            $color_Ster = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));            //添加雪花,位置隨機--參數(圖片資源,傾斜角度,左下角-x,左下角-y,顏色,字串)            imagestring($this->img,mt_rand(0,360),mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),‘*‘,$color_Ster);        }    }        private function addfont(){        for ($i = 0; $i < $this->code_num; $i++) {            //隨機從字元列表中取一個字元            $code = substr(str_shuffle($this->charlist),-1);            //記錄到驗證碼字串中            $this->codes .= $code;            //建立隨機顏色--參數(圖片資源,R,B,G)            $color_font = imagecolorallocate($this->img, mt_rand(10, 180), mt_rand(10, 180), mt_rand(10, 180));            //添加雪花,位置隨機--參數(圖片資源,字型大小,傾斜角度,左下角-x,左下角-y,字型顏色,字型,字串)            // 左下角-y,字型的基準高度是估計的,由於字型大小使用磅,不同字元的長寬像素相差甚大            imagettftext($this->img, $this->font_size, mt_rand(-30, 30), ($this->img_length/$this->code_num)*$i+mt_rand(1,$this->font_size*0.2), $this->img_height*0.7+mt_rand(-$this->img_height*0.2, $this->img_height*0.2), $color_font, $this->ttf, $code);        }    }        //輸出圖片至瀏覽器    private function  outputImg(){        //通知瀏覽器是png格式        header(‘Content-type:image/png‘);        //以png格式輸出        imagepng($this->img);        //銷毀記憶體中的圖片資源        imagedestroy($this->img);    }        public function __set($key,$value){            }        public function __get($value){            }        }


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.