/**
* GD库类
* 功能:水印 缩略图 验证码*/
classImageTool{protected static $erroTxt;/**
* getImgInfo
* 获取图片信息
* param filename
* return Array/False*/
protected static function getImgInfo($filename){if (!file_exists($filename)) {#code...
ImageTool::$erroTxt = $filename.‘图片不存在‘;return false;
}$imgInfo = getimagesize($filename);if(!$imgInfo){
ImageTool::$erroTxt = $filename.‘未知图片类型‘;return false;
}$info[‘width‘] = $imgInfo[0];$info[‘height‘] = $imgInfo[1];$info[‘ext‘] = strtolower( str_replace(‘/‘, ‘‘, strrchr($imgInfo[‘mime‘],‘/‘)) );return $info;
}/**
* water()
* 功能:图片加水印
* param : $dst_img 目标图片
* $water_img 水印图片
* $save 目标函数保存路径
* $pos 水印位置 上右下左 0 1 2 3 默认为2右下角
* $alpha透明度
* return*/
public static function water($dst_img,$water_img,$save=‘‘,$pos=‘2‘,$alpha=‘50‘){$dst_info = ImageTool::getImgInfo($dst_img);$water_info = ImageTool::getImgInfo($water_img);if(!$dst_info || !$water_info){return false;
}//判断水印图片是否大于目标图片
if ($dst_info[‘width‘]<&#61;$water_info[‘width‘] || $dst_info[‘height‘]<&#61;$water_info[‘height‘]) {#code...
ImageTool::$erroTxt&#61;‘水印图片大于待操作图片‘;return false;
}//生产对应画布函数
$createDstFun &#61; ‘imagecreatefrom‘.$dst_info[‘ext‘];$createWaterFun &#61; ‘imagecreatefrom‘.$water_info[‘ext‘];//return $createWaterFun;
if (!function_exists($createDstFun)) {#code...
ImageTool::$erroTxt&#61;$createDstFun.‘函数异常‘;return false;
}if (!function_exists($createWaterFun)) {#code...
ImageTool::$erroTxt&#61;$createWaterFun.‘函数异常‘;return false;
}//创建画布
// ob_clean();
$dst_im &#61; $createDstFun($dst_img);$water_im &#61; $createWaterFun($water_img);//画布函数操作
$src_x &#61; 0;$src_y &#61; 0;$src_w &#61; $water_info[‘width‘];$src_h &#61; $water_info[‘height‘];switch ($pos) {case ‘0‘:
#code...左上角
$dst_x &#61; 0;$dst_y &#61; 0;break;case ‘1‘:
#code...右上角
$dst_x &#61; $dst_info[‘width‘]-$water_info[‘width‘];$dst_y &#61; 0;break;case ‘3‘:
#code...左下角
$dst_x &#61; 0;$dst_y &#61; $dst_info[‘height‘]-$water_info[‘height‘];break;default:
#code...右下角
$dst_x &#61; $dst_info[‘width‘]-$water_info[‘width‘];$dst_y &#61; $dst_info[‘height‘]-$water_info[‘height‘];break;
}//return $pos;
$rs &#61; imagecopymerge ( $dst_im , $water_im , $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h , $alpha);if( !$rs){
ImageTool::$erroTxt&#61;‘水印添加失败‘;return false;
}$imgInputFun&#61;‘image‘.$dst_info[‘ext‘];if(!$save){$save &#61; $dst_img;unlink($dst_img);
}$imgInputFun($dst_im,$save);
imagedestroy($dst_im);
imagedestroy($water_im);return true;
}/**
* thumb()
* 缩略图
* param :
* $src_img 原图片路径
* $thumb_w,$thumb_h 缩略图宽高
* $save 缩略图保存路径*/
public static function thumb($src_img,$thumb_w&#61;‘200‘,$thumb_h&#61;‘200‘,$save&#61;‘‘){$thumbInfo &#61; ImageTool::getImgInfo($src_img);if(!$thumbInfo){return false;
}//创建画布
$dst_im &#61; imagecreatetruecolor($thumb_w,$thumb_h);//缩略图函数
$thumbFun &#61; ‘imagecreatefrom‘.$thumbInfo[‘ext‘];$thumb_im &#61; $thumbFun($src_img);//缩略图比例
$percent &#61; min( $thumb_w/$thumbInfo[‘width‘],$thumb_h/$thumbInfo[‘height‘] ); //小数
$bg &#61; imagecolorallocate($dst_im, 255, 255, 255);
imagefill($dst_im, 0, 0, $bg); //填充背景为白色
$dst_image &#61; $dst_im;$src_image &#61; $thumb_im;$src_x &#61; 0;$src_y &#61; 0;$dst_w &#61; $thumbInfo[‘width‘]*$percent; //图片粘贴到缩略图的宽
$dst_h &#61; $thumbInfo[‘height‘]*$percent; //图片粘贴到缩略图的高
$src_w &#61; $thumbInfo[‘width‘];$src_h &#61; $thumbInfo[‘height‘];$dst_x &#61; ($thumb_w - $dst_w)/2;$dst_y &#61; ($thumb_w - $dst_h)/2;$rs &#61; imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);if(!$rs){
ImageTool::$erroTxt&#61;‘生成缩略图失败‘;return false;
}$imgInputFun&#61;‘image‘.$thumbInfo[‘ext‘];if(!$save){//$path &#61; str_replace(‘D:/WWW/study/food/‘, ‘../‘, dirname($src_img)).‘/thumb_‘.basename($src_img);
$path &#61; dirname($src_img).‘/thumb_‘.basename($src_img);
}else{//$path &#61; str_replace(‘D:/WWW/study/food/‘, ‘../‘, dirname($src_img)).‘/‘.$save.‘_‘.basename($src_img);
$path &#61; dirname($src_img).‘/‘.$save.‘_‘.basename($src_img);
}$save &#61; $path;$imgInputFun($dst_im,$save);
imagedestroy($dst_im);return $path;
}/**
* captcha
* 生成验证码
* param : $width 验证码宽 $heigth 验证码高
* $lineNum 线条干扰码数目
* $alpha 线条干扰码透明度
* $length 验证码字符长度
* return String*/
public static function captchat($width&#61;‘70‘,$height&#61;‘25‘,$lineNum&#61;‘10‘,$length&#61;‘4‘,$alpha&#61;‘80‘){$capt_im &#61; imagecreatetruecolor($width, $height);$bak_im &#61; imagecreatetruecolor($width, $height);$bg &#61; imagecolorallocate($capt_im, 100, 100, 100);$color &#61; imagecolorallocate($capt_im, rand(0,255), rand(0,255), rand(0,255));
imagefill($capt_im, 0, 0, $bg);
imagefill($bak_im, 0, 0, $bg);//干扰线
for($i&#61;0;$i
imageline($capt_im, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $lincolor);
}//干扰点
for($i&#61;0;$i<200;$i&#43;&#43;){$dotcolor &#61; imagecolorallocatealpha($capt_im, rand(0,255), rand(0,255), rand(0,255),$alpha);
imagesetpixel($capt_im,rand(0,$width), rand(0,$height), $dotcolor);
}$size &#61;18;$angle &#61; 0;$x &#61; 2;$y &#61; 22;$fontfile &#61; ‘verdana.ttf‘;$text &#61; ImageTool::randName($length);
imagettftext($capt_im, $size, $angle, $x, $y, $color, $fontfile, $text);//验证码扭曲
$range &#61; 4; //扭曲程度
// imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
for($i&#61;0;$i
imagecopyresampled($bak_im, $capt_im, $i, $y, $i, 0, $i, $height, $i, $height);
}header("Content-type:image/png");
imagepng($bak_im);
imagedestroy($capt_im);
imagedestroy($bak_im);return true;
}/**
* 生成随机字符
* arg $n
* return string*/
protected static function randName($n&#61;6){ //$n 代表截取长度
$str &#61; ‘abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789‘;$str &#61; str_shuffle($str); //随机打乱一个字符串
$str &#61; substr($str,0,$n); //截取子字符串
return $str;
}/**
* getError()
* 获取错误信息
* param String
* return String*/
public static functiongetError(){return ImageTool::$erroTxt;
}
}//ImageTool::captchat();
?>