其實(shí)里面沒必要封裝函數(shù),只是當(dāng)時(shí)覺得視覺上好看而已,結(jié)構(gòu)清晰點(diǎn)
成都創(chuàng)新互聯(lián)專注于隨縣網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供隨縣營銷型網(wǎng)站建設(shè),隨縣網(wǎng)站制作、隨縣網(wǎng)頁設(shè)計(jì)、隨縣網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造隨縣網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供隨縣網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
<?php class captcha{ //驗(yàn)證碼-字符串 private $codes; //圖片長度 private $img_length = 150; //圖片高度 private $img_height = 30; //字符列表,用以生成隨機(jī)驗(yàn)證碼 private $charlist = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'; //隨機(jī)碼的個(gè)數(shù) private $code_num = 4; //字體大小--初始化時(shí)算出的 private $font_size ; //干擾線數(shù)目 private $line_num = 5; //干擾雪花數(shù)目 private $sterisk_num = 50; //驗(yàn)證碼--圖片 private $img; //字體文件路徑 private $ttf = './instance/font/Elephant.ttf'; public function __construct(){ //字體大小通過圖片寬高動(dòng)態(tài)生成的,但感覺不太完美 $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(){ //創(chuàng)建圖片資源 $this->createImage(); //往圖片中添加雪花 $this->addaSterisk(); //往圖片中添加字符 $this->addfont(); //往圖片中添加線條 $this->addLine(); //將圖片輸出至瀏覽器 $this->outputImg(); } //返回驗(yàn)證碼字符串 public function getCode(){ return $this->codes; } //創(chuàng)建圖片資源 private function createImage(){ //創(chuàng)建圖片資源 $this->img = p_w_picpathcreatetruecolor($this->img_length,$this->img_height); //創(chuàng)建顏色 $color_bg = p_w_picpathcolorallocate($this->img, mt_rand(210, 255), mt_rand(210, 255), mt_rand(210, 255)); //設(shè)置圖片背景色 p_w_picpathfill($this->img, 0, 0, $color_bg); } //往圖片中添加線條 private function addLine(){ //添加指定數(shù)量的線條 for ($i = 0; $i < $this->line_num; $i++) { //創(chuàng)建隨機(jī)顏色--參數(shù)(圖片資源,R,B,G) $color_line = p_w_picpathcolorallocate($this->img, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200)); //添加線條,位置隨機(jī)--參數(shù)(圖片資源,起點(diǎn)-x,起點(diǎn)-y,終點(diǎn)-x,終點(diǎn)-y,顏色) //不可調(diào)整 //p_w_picpathline($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); //可以調(diào)整線條的粗細(xì) $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++) { p_w_picpathline($this->img, $src_x+$j, $src_y+$j, $dest_x+$j,$dest_y+$j, $color_line); } } } //往圖片中添加雪花 private function addaSterisk(){ //添加指定數(shù)量的雪花 for ($i = 0; $i < $this->sterisk_num; $i++) { //創(chuàng)建隨機(jī)顏色--參數(shù)(圖片資源,R,B,G) $color_Ster = p_w_picpathcolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); //添加雪花,位置隨機(jī)--參數(shù)(圖片資源,傾斜角度,左下角-x,左下角-y,顏色,字符串) p_w_picpathstring($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++) { //隨機(jī)從字符列表中取一個(gè)字符 $code = substr(str_shuffle($this->charlist),-1); //記錄到驗(yàn)證碼字符串中 $this->codes .= $code; //創(chuàng)建隨機(jī)顏色--參數(shù)(圖片資源,R,B,G) $color_font = p_w_picpathcolorallocate($this->img, mt_rand(10, 180), mt_rand(10, 180), mt_rand(10, 180)); //添加雪花,位置隨機(jī)--參數(shù)(圖片資源,字體大小,傾斜角度,左下角-x,左下角-y,字體顏色,字體,字符串) // 左下角-y,字體的基準(zhǔn)高度是估計(jì)的,由于字體大小使用磅,不同字符的長寬像素相差甚大 p_w_picpathttftext($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:p_w_picpath/png'); //以png格式輸出 p_w_picpathpng($this->img); //銷毀內(nèi)存中的圖片資源 p_w_picpathdestroy($this->img); } public function __set($key,$value){ } public function __get($value){ } }
標(biāo)題名稱:PHP碎碼——自己寫的驗(yàn)證碼
瀏覽地址:http://jinyejixie.com/article8/ijggip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)