<?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
?>