验证码---H_img.php
生活随笔
收集整理的這篇文章主要介紹了
验证码---H_img.php
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?php
/*************************************
* 驗證碼---H_img.php
* 創建人: huang_xiang
* 創建時間:2008-12-25 9:29
* 更新時間:2009-2-18 18:15
* Email:yin273642232@163.com QQ:273642232
*************************************/
/*************************************
實例代碼include_once("H_img.php"); //引入文件$H_img = new H_img();$H_img -> H_font_file = "H_S/ttf/JTDS.ttf";//字體$H_img -> H_width = 130; //圖片寬$H_img -> H_heigt = 38; //圖片高$H_img -> H_bg = "white"; //背景色(可以是rgb數組);$H_img -> H_f_ange = -1; //字體角度(默認為-1 隨機)$H_img -> H_f_x = 10; //字符x坐標(默認10)$H_img -> H_f_y = 30; //字符y坐標(默認30)$H_img -> H_fd = 0; //是否浮動(默認0不浮動)$H_img -> H_pixl_num = 10; //干擾點數$H_img -> H_px = array(0,100,100); //干擾點背景色$H_img -> H_line_num = 3; //干擾線數$H_img -> H_li = array(100,100,100); //干擾線背景色$H_img -> H_f_size = 18; //字體大小(默認16)$H_img -> H_ft = "rand"; //字符顏色(可以是rgb數組)$H_img -> H_bgurl = "0001.jpg"; //背景圖片$H_img -> H_bgurl_flag = 0; //背景圖片(0為默認大小)$H_img -> H_bgurl_w = 0; //背景圖片寬$H_img -> H_bgurl_h = 0; //背景圖片高$H_img -> H_bgurl_wh = array(50,30); //背景圖片的起點寬高$H_img -> H_bordr = 1; //邊框寬度(默認沒有邊框)$H_img -> H_bordr_color = array("black","black","black","black"); //邊框顏色(默認白色上、右、下、左)// gif驗證碼$H_img -> H_gifda(array(array(4,"Aa"),array(4,"0Z"),array(4,"aA"),array(4,"a0"),array(4,"AZ"),array(4,"0aA")),$H_jgtm=1,$H_entm=1);$H_img -> H_dispy_gif();// jpg驗證碼$H_img -> H_getimg(4,"ZAa0"); //字符集(第一個參數是字符數or中文數,Z表示含有中文、A表示含有大寫字母、a表示含有小寫字母、0表示含有數字)$H_img -> H_dispy();$H_img -> H_getimg($H_size,$H_type); //返回:返回圖片,參數:$H_size-文字個數(中文也是1).$H_type-文字類型("AaZ0")$H_img -> get_rgb($H_color); //返回:返回顏色rgb數組,參數:$H_color-顏色單詞$H_img -> H_get_utf8($H_str); //返回:判斷是否是utf-8字符,參數:$H_str-需要判斷的字符串
*************************************/
//Start Class
class H_img{public $H_debug = false;public $H_desce; //描述public $H_ROOT_DIR; //文件地址路徑public $H_img;public $H_bgurl; //背景圖片public $H_bgurl_flag = 0; //背景圖片是否全部(默認不是全部圖片)public $H_bgurl_w; //背景圖片寬public $H_bgurl_h; //背景圖片高public $H_bgurl_wh=array(0,0); //背景圖片的起點寬高public $H_bordr = 0; //邊框寬度(默認沒有邊框)public $H_bordr_color = array("white","white","white","white"); //邊框顏色(默認白色上、右、下、左)public $H_width = 230; //圖片寬public $H_heigt = 50; //圖片高public $H_font_file = "H_S/ttf/JTDS.ttf"; //字體文件路徑public $H_bg = "white"; //圖片背景顏色(默認白色)public $H_px = "white"; //干擾點顏色(默認紫色)public $H_ft = "rand"; //字體顏色(默認隨機)public $H_li = "white"; //干擾線顏色(默認紫色)public $H_pixl_num = 0; //干擾點數public $H_line_num = 0; //干擾線數public $H_f_size = 16; //字體大小public $H_f_ange = -1; //字體角度(默認為隨機)public $H_fd = 0; //是否浮動(默認0不浮動)public $H_fd_y = 8; //浮動幅度(默認8)public $H_f_x = 10; //x坐標public $H_f_y = 30; //y坐標public $H_zf_x = 30; //字間距public $H_img_str; //顯示的文字(array)public $H_img_font=""; //顯示的文字public $H_china_str = "的一是在了不和有大這主中人上為們地個用工時要動國產以我到他會作來分生對于學下級就年階義發成部民可出能方進同行面說種過命度革而多子后自社加小機也經力線本電高量長黨得實家定深法表著水理化爭現所二起政三好十戰無農使性前等反體合斗路圖把結第里正新開論之物從當兩些還天資事隊批如應形想制心樣干都向變關點育重其思與間內去因件日利相由壓員氣業代全組數果期導平各基或月毛然問比展那它最及外沒看治提五解系林者米群頭意只明四道馬認次文通但條較克又公孔領軍流入接席位情運器并飛原油放立題質指建區驗活眾很教決特此常石強極土少已根共直團統式轉別造切九你取西持總料連任志觀調七么山程百報更見必真保熱委手改管處己將修支識病象幾先老光專什六型具示復安帶每東增則完風回南廣勞輪科北打積車計給節做務被整聯步類集號列溫裝即毫知軸研單色堅據速防史拉世設達爾場織歷花受求傳口斷況采精金界品判參層止邊清至萬確究書術狀廠須離再目海交權且兒青才證低越際八試規斯近注辦布門鐵需走議縣兵固除般引齒千勝細影濟白格效置推空配刀葉率述今選養德話查差半敵始片施響收華覺備名紅續均藥標記難存測士身緊液派準斤角降維板許破述技消底床田勢端感往神便賀村構照容非搞亞磨族火段算適講按值美態黃易彪服早班麥削信排臺聲該擊素張密害侯草何樹肥繼右屬市嚴徑螺檢左頁抗蘇顯苦英快稱壞移約巴材省黑武培著河帝僅針怎植京助升王眼她抓含苗副雜普談圍食射源例致酸舊卻充足短劃劑宣環落首尺波承粉踐府魚隨考刻靠夠滿夫失包住促枝局菌桿周護巖師舉曲春元超負砂封換太模貧減陽揚江析畝木言球朝醫校古呢稻宋聽唯輸滑站另衛字鼓剛寫劉微略范供阿塊某功套友限項余倒卷創律雨讓骨遠幫初皮播優占死毒圈偉季訓控激找叫云互跟裂糧粒母練塞鋼頂策雙留誤礎吸阻故寸盾晚絲女散焊功株親院冷徹彈錯散商視藝滅版烈零室輕血倍缺厘泵察絕富城沖噴壤簡否柱李望盤磁雄似困鞏益洲脫投送奴側潤蓋揮距觸星松送獲興獨官混紀依未突架寬冬章濕偏紋吃執閥礦寨責熟穩奪硬價努翻奇甲預職評讀背協損棉侵灰雖矛厚羅泥辟告卵箱掌氧恩愛停曾溶營終綱孟錢待盡俄縮沙退陳討奮械載胞幼哪剝迫旋征槽倒握擔仍呀鮮吧卡粗介鉆逐弱腳怕鹽末陰豐編印蜂急拿擴傷飛露核緣游振操央伍域甚迅輝異序免紙夜鄉久隸缸夾念蘭映溝乙嗎儒殺汽磷艱晶插埃燃歡鐵補咱芽永瓦傾陣碳演威附牙芽永瓦斜灌歐獻順豬洋腐請透司危括脈宜笑若尾束壯暴企菜穗楚漢愈綠拖牛份染既秋遍鍛玉夏療尖殖井費州訪吹榮銅沿替滾客召旱悟刺腦措貫藏敢令隙爐殼硫煤迎鑄粘探臨薄旬善福縱擇禮愿伏殘雷延煙句純漸耕跑澤慢栽魯赤繁境潮橫掉錐希池敗船假亮謂托伙哲懷割擺貢呈勁財儀沉煉麻罪祖息車穿貨銷齊鼠抽畫飼龍庫守筑房歌寒喜哥洗蝕廢納腹乎錄鏡婦惡脂莊擦險贊鐘搖典柄辯竹谷賣亂虛橋奧伯趕垂途額壁網截野遺靜謀弄掛課鎮妄盛耐援扎慮鍵歸符慶聚繞摩忙舞遇索顧膠羊湖釘仁音跡碎伸燈避泛亡答勇頻皇柳哈揭甘諾概憲濃島襲誰洪謝炮澆斑訊懂靈蛋閉孩釋乳巨徒私銀伊景坦累勻霉杜樂勒隔彎績招紹胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗堿殊崗挖氏刃劇堆赫荷胸衡勤膜篇登駐案刊秧緩凸役剪川雪鏈漁啦臉戶洛孢勃盟買楊宗焦賽旗濾硅炭股坐蒸凝竟陷槍黎救冒暗洞犯筒您宋弧爆謬涂味津臂障褐陸啊健尊豆拔莫抵桑坡縫警挑污冰柬嘴啥飯塑寄趙喊墊康遵牧遭幅園腔訂香肉弟屋敏恢忘衣孫齡嶺騙休借丹渡耳刨虎筆稀昆浪薩茶滴淺擁穴覆倫娘噸浸袖珠雌媽紫戲塔錘震歲貌潔剖牢鋒疑霸閃埔猛訴刷狠忽災鬧喬唐漏聞沈熔氯荒莖男凡搶像漿旁玻亦忠唱蒙予紛捕鎖尤乘烏智淡允叛畜俘摸銹掃畢璃寶芯爺鑒秘凈蔣鈣肩騰枯拋軌堂拌爸循誘祝勵肯酒繩窮塘燥泡袋朗喂鋁軟渠顆慣貿糞綜墻趨彼屆墨礙啟逆卸航霧冠丙街萊貝輻腸付吉滲瑞驚頓擠秒懸姆爛森糖圣凹陶詞遲蠶億矩";public $H_zimu_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";public $H_num_str = "0123456789";public $H_gif; //處理gif對象public $H_imgda = array(); //圖數據public $H_imgsm = 0; //次數public $H_dly = array(); //延時public $H_lop = 0; //循環次數public $H_mod = "bin"; //數據源模式public $H_img_gif; //圖像public $H_gif_img_font; //顯示的文字public $H_bordf_color = array(); //邊框成型顏色public function __construct(){//當實例化一個對象的時候,這個對象的這個方法首先被調用 return '';}public function __destruct(){//當刪除一個對象或對象操作終止的時候,調用該方法return '';}public function __get($key){//當試圖讀取一個并不存在的屬性的時候被調用return '['.$key.'] Variable not find';}public function __set($key,$val){//當試圖向一個并不存在的屬性寫入值的時候被調用return '['.$key.'] Variable not find';}public function __call($key,$args){//當試圖調用一個對象并不存在的方法時,調用該方法return '['.$key.'] Function not find';}public function __toString(){//當打印一個對象的時候被調用return $this -> H_desce();}public function __clone(){//當對象被克隆時,被調用return "clone";}public function H_desce(){//返回描述$this -> H_desce .= '類名:H_img-驗證碼;';$this -> H_desce .= '函數:H_getimg($H_size,$H_type),返回:圖片,參數:$H_size-文字個數(中文也是1).$H_type-文字類型("AaZ0");';$this -> H_desce .= '函數:get_rgb($H_color),返回:顏色rgb數組,參數:$H_color-顏色單詞;';$this -> H_desce .= '函數:H_get_utf8($H_str),返回:判斷是否是utf-8字符,參數:$H_str-需要判斷的字符串;';return $this -> H_desce;}public function H_getimg($H_size,$H_type){//返回圖片$this -> H_bg = is_array($this -> H_bg) == true ? $this -> H_bg : $this -> get_rgb($this -> H_bg);$this -> H_px = is_array($this -> H_px) == true ? $this -> H_px : $this -> get_rgb($this -> H_px);$this -> H_ft = is_array($this -> H_ft) == true ? $this -> H_ft : $this -> get_rgb($this -> H_ft);$this -> H_li = is_array($this -> H_li) == true ? $this -> H_li : $this -> get_rgb($this -> H_li);$this -> H_img = imagecreate($this -> H_width,$this -> H_heigt); $H_bg_color = imagecolorallocate($this -> H_img,$this -> H_bg[0],$this -> H_bg[1],$this -> H_bg[2]);$H_pixl_color = imagecolorallocate($this -> H_img,$this -> H_px[0],$this -> H_px[1],$this -> H_px[2]);$H_line_color = imagecolorallocate($this -> H_img,$this -> H_li[0],$this -> H_li[1],$this -> H_li[2]);$H_font_color = is_array($this -> H_ft) == true ? imagecolorallocate($this -> H_img,$this -> H_ft[0],$this -> H_ft[1],$this -> H_ft[2]) : "";//設置邊框顏色if($this -> H_bordr > 0){$this -> H_bordr_color[0] = is_array($this -> H_bordr_color[0]) == true ? $this -> H_bordr_color[0] : $this -> get_rgb($this -> H_bordr_color[0]);$this -> H_bordr_color[1] = is_array($this -> H_bordr_color[1]) == true ? $this -> H_bordr_color[1] : $this -> get_rgb($this -> H_bordr_color[1]);$this -> H_bordr_color[2] = is_array($this -> H_bordr_color[2]) == true ? $this -> H_bordr_color[2] : $this -> get_rgb($this -> H_bordr_color[2]);$this -> H_bordr_color[3] = is_array($this -> H_bordr_color[3]) == true ? $this -> H_bordr_color[3] : $this -> get_rgb($this -> H_bordr_color[3]);$this -> H_bordf_color[0] = $this -> H_bordr_color[0] == "rand" ? imagecolorallocate($this -> H_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)):imagecolorallocate($this -> H_img,$this -> H_bordr_color[0][0],$this -> H_bordr_color[0][1],$this -> H_bordr_color[0][2]);$this -> H_bordf_color[1] = $this -> H_bordr_color[1] == "rand" ? imagecolorallocate($this -> H_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)):imagecolorallocate($this -> H_img,$this -> H_bordr_color[1][0],$this -> H_bordr_color[1][1],$this -> H_bordr_color[1][2]);$this -> H_bordf_color[2] = $this -> H_bordr_color[2] == "rand" ? imagecolorallocate($this -> H_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)):imagecolorallocate($this -> H_img,$this -> H_bordr_color[2][0],$this -> H_bordr_color[2][1],$this -> H_bordr_color[2][2]);$this -> H_bordf_color[3] = $this -> H_bordr_color[3] == "rand" ? imagecolorallocate($this -> H_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)):imagecolorallocate($this -> H_img,$this -> H_bordr_color[3][0],$this -> H_bordr_color[3][1],$this -> H_bordr_color[3][2]);}//產生文字$H_t1 = strpos("my".$H_type,"A");$H_t2 = strpos("my".$H_type,"a");$H_t3 = strpos("my".$H_type,"0");$H_t4 = strpos("my".$H_type,"Z");$H_temp_str = '';$H_temp_str .= $H_t4 > 0 ? $this -> H_china_str : "";$H_temp_str .= $H_t1 > 0 ? substr($this -> H_zimu_str,0,26) : "";$H_temp_str .= $H_t2 > 0 ? substr($this -> H_zimu_str,26,26) : "";$H_temp_str .= $H_t3 > 0 ? $this -> H_num_str : "";$H_temp_len = strlen($H_temp_str);$H_china_num = strlen($this -> H_china_str);$H_zi = 0;while(count($this -> H_img_str) < $H_size){$H_rand = rand(0,$H_temp_len-1);$H_rand = $H_t4 > 0 ? ($H_rand >= $H_china_num ? $H_rand : ($H_rand%2 == 0 ? $H_rand : $H_rand+1)) : $H_rand;$H_ii = ord(substr($H_temp_str,$H_rand,1)) > 0xa0 ? 2 : 1;$H_zi += $H_ii > 1 ? 1 : 0; //中文數$H_font_coary[] = imagecolorallocate($this -> H_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));if(substr($H_temp_str,$H_rand,$H_ii) != "") $this -> H_img_font .= $this -> H_img_str[] = iconv("GB2312","UTF-8//IGNORE",substr($H_temp_str,$H_rand,$H_ii));}if($this -> H_bgurl != ''){//背景圖片$H_imgxx = getImageSize($this -> H_bgurl);$H_imgxw = $this -> H_width;$H_imgxh = $this -> H_heigt;switch($H_imgxx[2]){case "1" :{Header("Content-type: image/GIF");$H_temg = ImageCreateFromGIF($this -> H_bgurl);break;}case "2" :{Header("Content-type: image/JPEG");$H_temg = ImageCreateFromJPEG($this -> H_bgurl);break;}case "3" :{Header("Content-type: image/PNG");$H_temg = ImageCreateFromPNG($this -> H_bgurl);break;}}if($this -> H_bgurl_flag != '0'){$H_imgxw = $H_imgxx[1];$H_imgxh = $H_imgxx[0];$H_imgxw = $this -> H_bgurl_w == '' ? $H_imgxw : $this -> H_bgurl_w;$H_imgxh = $this -> H_bgurl_h == '' ? $H_imgxh : $this -> H_bgurl_h;}imagecopyresized($this -> H_img,$H_temg,0,0,$this -> H_bgurl_wh[0],$this -> H_bgurl_wh[1],$this -> H_width,$this -> H_heigt,$H_imgxw,$H_imgxh);}//繪制干擾點for($H_i = 0;$H_i < $this -> H_pixl_num;$H_i++) imagesetpixel($this -> H_img,mt_rand(0,$this -> H_width),mt_rand(0,$this -> H_heigt),$H_pixl_color);//繪制干擾線for($H_i = 0;$H_i < $this -> H_line_num;$H_i++) imageline($this -> H_img,mt_rand(0,$this -> H_width),mt_rand(0,$this -> H_heigt),mt_rand(0,$this -> H_width),mt_rand(0,$this -> H_heigt),$H_line_color);//繪制字符$H_tem_y = $this -> H_f_y;$H_tem_x = $this -> H_f_x;$H_i = 0;foreach($this -> H_img_str as $H_temp){$H_f_ange = $this -> H_f_ange == -1 ? mt_rand(-45,46) : $this -> H_f_ange; //字體角度(水平為0)$H_font_color = $this -> H_ft == "rand" ? $H_font_coary[$H_i] : $H_font_color;ImageTTFText($this -> H_img,$this -> H_f_size,$H_f_ange,$this -> H_f_x,$this -> H_f_y,$H_font_color,$this -> H_font_file,$H_temp);$this -> H_f_x = $this -> H_f_x + $this -> H_zf_x;$this -> H_f_y = $this -> H_fd == 1 ? $H_tem_y +($this -> H_fd_y * mt_rand(-1,1)) : $H_tem_y;++$H_i;}//繪制邊框if($this -> H_bordr > 0){$H_temp = 0;while($H_temp < $this -> H_bordr){imageline($this -> H_img,0,0+$H_temp,$this -> H_width,0+$H_temp,$this -> H_bordf_color[0]); //上imageline($this -> H_img,$this -> H_width-1-$H_temp,0,$this -> H_width-1-$H_temp,$this -> H_heigt,$this -> H_bordf_color[1]); //右imageline($this -> H_img,$this -> H_width,$this -> H_heigt-1-$H_temp,0,$this -> H_heigt-1-$H_temp,$this -> H_bordf_color[2]); //下imageline($this -> H_img,0+$H_temp,$this -> H_heigt,0+$H_temp,0,$this -> H_bordf_color[3]); //左++$H_temp;}}}//返回顏色rgb數組public function get_rgb($H_color){switch($H_color){case "red" : { return array(255,0,0); break; } //紅色case "orange" : { return array(255,122,0); break; } //橙色case "yellow" : { return array(255,255,0); break; } //黃色case "green" : { return array(0,255,0); break; } //綠色case "qgreen" : { return array(0,255,255); break; } //青色case "blue" : { return array(0,0,255 ); break; } //藍色case "purple" : { return array(255,0,255); break; } //紫色case "white" : { return array(255,255,255); break; } //白色case "black" : { return array(0,0,0); break; } //黑色case "mtrand" : { return array(mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); break; } //隨機色}return "rand";}//判斷是否是utf-8字符public function H_get_utf8($H_str){return preg_match('%^(?:[\x09\x0A\x0D\x20-\x7E] # ASCII| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16)*$%xs', $H_str);}//顯示圖片(gif 動態)public function H_gifda($H_array=array(array(4,"Aa0Z")),$H_jgtm=0.1,$H_entm=1){$H_temp_f_x = $this -> H_f_x;$H_temp_f_y = $this -> H_f_y;ob_start();$H_gifi = 0;while($H_gifi < count($H_array)){$this -> H_f_x = $H_temp_f_x;$this -> H_f_y = $H_temp_f_y;$this -> H_getimg($H_array[$H_gifi][0],$H_array[$H_gifi][1]);imagegif($this -> H_img);imagedestroy($this -> H_img);$this -> H_img_str = array();$this -> H_imgda[$H_gifi] = ob_get_contents();ob_clean();if($H_gifi == count($H_array)-1) $this -> H_dly[] = $H_entm*100; //循環的間隔時間(1000/100=10 s) -1:結束else $this -> H_dly[] = $H_jgtm*100; //每幀的間隔時間 十分之一秒 -1:結束++$H_gifi;}$this -> H_gif = new H_gif();//數據源 延時 循環次數 透明度 r g b "bin/url"$this -> H_gif -> H_set_gif($this -> H_imgda,$this -> H_dly,$this -> H_lop,2,$H_r=0,$H_g=0,$H_b=0,$this -> H_mod);$this -> H_img_gif = $this -> H_gif -> GetAnimation();}//顯示圖片public function H_dispy($H_type="png",$H_dest=1){header('Content-type: image/'.$H_type);switch($H_type){case "jpeg" : { imagejpeg($this -> H_img); break; }case "png" : { imagepng($this -> H_img); break; }case "gif" : { imagegif($this -> H_img); break; }default : imagepng($this -> H_img);}if($H_dest == 1) imagedestroy($this -> H_img);}//存儲圖片public function H_store($H_urlnm="temp",$H_type="png",$H_dest=1){switch($H_type){case "jpeg" : { imagejpeg($this -> H_img,$H_urlnm.".jpg"); break; }case "png" : { imagepng($this -> H_img,$H_urlnm.".png"); break; }case "gif" : { imagegif($this -> H_img_gif,$H_urlnm.".gif"); break; }default : imagepng($this -> H_img,$H_urlnm.".png");}if($H_dest == 1 && $H_type != "gif") imagedestroy($this -> H_img);else imagedestroy($this -> H_img_gif);}//輸出public function H_dispy_gif(){Header('Content-type:image/gif');echo $this -> H_img_gif;}
}//End Class
?>
?
轉載于:https://www.cnblogs.com/huangxiang/archive/2012/01/17/2324705.html
總結
以上是生活随笔為你收集整理的验证码---H_img.php的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自己有電腦的一定要看看,非常有用呢。
- 下一篇: MySQL学习笔记03-MySQL的安装