其實里面沒必要封裝函數(shù),只是當(dāng)時覺得視覺上好看而已,結(jié)構(gòu)清晰點

創(chuàng)新互聯(lián)公司是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷、企業(yè)網(wǎng)站建設(shè),
賣友情鏈接,
廣告投放為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計到用戶體驗提高,創(chuàng)新互聯(lián)力求做到盡善盡美。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();
}
//返回驗證碼字符串
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)建隨機顏色--參數(shù)(圖片資源,R,B,G)
$color_line = p_w_picpathcolorallocate($this->img, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
//添加線條,位置隨機--參數(shù)(圖片資源,起點-x,起點-y,終點-x,終點-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)整線條的粗細
$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)建隨機顏色--參數(shù)(圖片資源,R,B,G)
$color_Ster = p_w_picpathcolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
//添加雪花,位置隨機--參數(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++) {
//隨機從字符列表中取一個字符
$code = substr(str_shuffle($this->charlist),-1);
//記錄到驗證碼字符串中
$this->codes .= $code;
//創(chuàng)建隨機顏色--參數(shù)(圖片資源,R,B,G)
$color_font = p_w_picpathcolorallocate($this->img, mt_rand(10, 180), mt_rand(10, 180), mt_rand(10, 180));
//添加雪花,位置隨機--參數(shù)(圖片資源,字體大小,傾斜角度,左下角-x,左下角-y,字體顏色,字體,字符串)
// 左下角-y,字體的基準高度是估計的,由于字體大小使用磅,不同字符的長寬像素相差甚大
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){
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標題名稱:PHP碎碼——自己寫的驗證碼-創(chuàng)新互聯(lián)
鏈接分享:
http://weahome.cn/article/djopep.html