PHP隨機圖片產生類____PHP

來源:互聯網
上載者:User

  18日去日月潭和九族文化村玩了一天。玩本身就很累了,加之又是都是玩大型娛樂裝置,諸如雲霄飛車、太空船、UFO、海盜船、加勒比海盜等。總之,一句話就是到今天都沒恢複過來。不過今天還是死活完成了一些任務啦。哈。

 

  下面是關於PHP產生隨機圖片的類,直接copy就可以用的。

 

<?php /** * 產生驗證碼的類 * */ class code{ var $str; //隨機產生的字串 var $width = 85; //驗證碼圖片的寬度 var $height = 30; //驗證碼圖片的高度 /** * 建構函式 * * @param String $width 驗證碼圖片的寬度 * @param String $height 驗證碼圖片的高度 * @param String $size 字元個數 */ function code($width = 50,$height = 25,$size = 4){ $this->str = $this->random($size); $this->width = $width; $this->height = $height; //session_register("code"); $_SESSION["code"] = $this->str; } /** * 隨即產生字元的函數 * * @param int $len 要產生的字元的個數 * @return 產生的字串 */ function random($len){ $srcstr="abcdefghijklmnopqrstuvwxyz0123456789"; mt_srand(); $strs=""; for($i=0;$i<$len;$i++){ $strs.=$srcstr[mt_rand(0,35)]; } return $strs; } /** * 產生驗證碼並輸出 * */#7CD3E7 function genimg(){ @header("Content-Type:image/png"); $im=imagecreate($this->width,$this->height); //背景色 $back=imagecolorallocate($im,0xFF,0xFF,0xFF); //模糊點顏色 $pix=imagecolorallocate($im,187,230,247); //字型色 $font=imagecolorallocate($im,41,163,238); //繪模糊作用的點 mt_srand(); for($i=0;$i<1000;$i++){ imagesetpixel($im,mt_rand(0,$this->width),mt_rand(0,$this->height),$pix); } //寫字,選擇ComicSansMS字型 //imagettftext($im,20,0,3,25,$font,"comic.ttf",$this->str); //$x = mt_rand(1,20); //$y = mt_rand(0,18); imagestring($im, 8, 10, 5,$this->str, $font); imagerectangle($im,0,0,$this->width-1,$this->height-1,$font); imagepng($im,"imcode.png"); imagedestroy($im); Header("Content-type: text/html"); } } ?>

 

 

使用的例子如下:

 

首先在使用的PHP檔案裡引用以上類,然後new出對象。

$s = new code();
$s->genimg();

 

再在需要驗證碼的地方

<IMG SRC='<?php echo 'imcode.png?'.time();?>' WIDTH='50' HEIGHT='25' BORDER=0 ALT=''>

就OK了。

 

 

 

 

聯繫我們

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