簡介:這是php寫驗證碼類的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=341730' scrolling='no'>
前一段時間我寫了個驗證碼函數, 今天做成了驗證碼類 有助於物件導向編程。
img.php
1 font=ROOT_PATH.'/font/elephant.ttf';24 }25 //建立4個隨機碼26 private function createCode(){27 $_leng=strlen($this->charset);28 for($i=1;$i<=$this->codelen;$i++){29 $this->code.=$this->charset[mt_rand(0,$_leng)];30 }31 return $this->code;32 }33 34 //建立背景35 private function createBg(){36 //建立畫布 給一個資源jubing37 $this->img=imagecreatetruecolor($this->width,$this->height);38 //背景顏色39 $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));40 //畫出一個矩形41 imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);42 }43 44 //建立字型45 private function createFont(){46 $_x=($this->width / $this->codelen); //字型長度47 for ($i=0;$i<$this->codelen;$i++){48 //文字顏色49 $color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));50 //資源控制代碼 字型大小 傾斜度 字型長度 字型高度 字型顏色 字型 具體文本51 imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code[$i]);52 }53 }54 //隨機線條55 private function createLine(){56 //隨機線條57 for ($i=0;$i<6;$i++){58 $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));59 imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);60 }61 //隨機雪花62 for ($i=0;$i<45;$i++){63 $color = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));64 imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);65 }66 }67 //輸出背景68 private function outPut(){69 //產生標題70 header('ContentType:img/png');71 //輸出圖片72 imagepng($this->img);73 //銷毀結果集74 imagedestroy($this->img);75 }76 //對外輸出77 public function doimg(){78 //載入背景79 $this->createBg();80 //負載檔案81 $this->createCode();82 //載入線條83 $this->createLine();84 //載入字型85 $this->createFont();86 //載入背景87 $this->outPut();88 }89 90 //擷取驗證碼91 public function getCode(){92 return strtolower($this->code);93 }94 95 }96 97 ?>
其他頁面調用方法
index.php
require 'img.php';
$img=new ValidateCode();
echo $img->doimg();
?>
過往雲煙 2011/07/29
愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具
http://biancheng.dnbcw.info/php/341730.html pageNo:6