<?php 
class cls_pic
{
	private $perms;
    function __construct()
    {
		$this->perms=array();
    }

	function get_perms($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function set_perms($name,$value){
		$this->perms["$name"]=$value;
	}

	//生成缩图
	function on_resize($msg_source,$msg_w,$msg_h,$msg_to)
	{
		if($msg_source==""||!is_file($msg_source)) return "图片不存在！";
		if($msg_to=="") return "目标图片不能为空！";
		$str_source=$msg_source;
		$str_info="";
		$str_to=$msg_to;
		$int_w2=$msg_w;
		$int_h2=$msg_h;
		$int_data=getimagesize($str_source,$str_info);
		switch ($int_data[2])
		{
			case 1:
			{
				if(!function_exists("imagecreatefromgif")){
					return "图片系统函数不存在！";
				}
				$im=imagecreatefromgif($str_source);
				break;
			}
			case 2:
			{
				if(!function_exists("imagecreatefromjpeg")){
					return "图片系统函数不存在！";
				}
				$im=imagecreatefromjpeg($str_source);
				break;
			}
			case 3:
			{
				$im=imagecreatefrompng($str_source);
				break;
			}
			default:
				return "图片不支持此格式！";
		}
		$int_w=imagesx($im);
		$int_h=imagesy($im);
		if($int_h2<1){
			$int_h2=($int_w2*$int_h)/$int_w;
		}
		if($int_w2<1){
			$int_w2=($int_w*$int_h2)/$int_h;
		}
		if($int_w<$int_w2||$int_h<$int_h2) return "图片大小不对！";
		if(function_exists("imagecreatetruecolor"))
		{
			$ni=imagecreatetruecolor($int_w2,$int_h2);
			if($ni){
				imagecopyresampled($ni,$im,0,0,0,0,$int_w2,$int_h2,$int_w,$int_h);
			}else{
				$ni=imagecreate($int_w2,$int_h2);
				imagecopyresized($ni,$im,0,0,00,$int_w2,$int_h2,$int_w,$int_h);
			}
		}else{
			$ni=imagecreate($int_w2,$int_h2);
			imagecopyresized($ni,$im,0,0,0,0,$int_w2,$int_h2,$int_w,$int_h);
		}
		if(function_exists('imagejpeg')){
			imagejpeg($ni,$str_to);
		}else{
				imagepng($ni,$str_to);
			}
		imagedestroy($ni);
		imagedestroy($im);
		return "";
	}
	function get_watemark_pos($msg_val,$msg_w1,$msg_h1,$msg_w2,$msg_h2)
	{
		switch($msg_val)
		{
			case 0:
				$int_x = rand(0,($msg_w1 - $msg_w2));
				$int_y = rand(0,($msg_h1 - $msg_h2));
				break;
			case 1:
				$int_x = 5;
				$int_y = 5;
				break;
			case 2:
				$int_x = ($msg_w1 - $msg_w2) / 2;
				$int_y = 0;
				break;
			case 3:
				$int_x = $msg_w1 - $msg_w2;
				$int_y = 0;
				break;
			case 4:
				$int_x = 0;
				$int_y = $msg_h1/ 2;
				break;
			case 5:
				$int_x = ($msg_w1 - $msg_w2) / 2;
				$int_y = $msg_h1/ 2;
				break;
			case 6:
				$int_x = $msg_w1 - $msg_w2;
				$int_y = $msg_h1/2;
				break;
			case 7:
				$int_x = 0;
				$int_y = $msg_h1 - $msg_h2;
				break;
			case 8:
				$int_x = ($msg_w1 - $msg_w2) / 2;
				$int_y = $msg_h1 - $msg_h2;
				break;
			case 9:
				$int_x = $msg_w1 - $msg_w2;
				$int_y = $msg_h1 - $msg_h2;
				break;
			default:
				$int_x = rand(0,($msg_w1 - $msg_w2));
				$int_y = rand(0,($msg_h1 - $msg_h2));
				break;
		}
		$arr_val["x"]=$int_x;
		$arr_val["y"]=$int_y;
		return $arr_val;
	}
	//添加水印
	function on_font_watermark($msg_source,$msg_text='',$msg_pos=0,$msg_font_size = 10,$msg_color='#ff0000',$msg_quality=100,$msg_target='')
	{
		if(!is_file($msg_source)) return "图片不存在！";
		if($msg_text=="") return "水印文字不能为空！";
		if(!is_pic($msg_source)) return "只有图片才能加水印！";
		$pic_info=getimagesize($msg_source);
		switch($pic_info[2])
		{
			case 1 :
				break;
			case 2 :
				break;
			case 3 :
				break;
			default :
				return "图片格式不支持！";
		}
		($msg_target=="")? $str_target=$msg_source:$str_target=$msg_target;
		$int_w=$pic_info[0];
		$int_h=$pic_info[1];
		$font_path=$this->perms["font_path"];
		$font_text=iconv('gb2312','utf-8',$msg_text);
		$arr_font = imagettfbbox($msg_font_size, 0, $font_path, $font_text);
		$int_w1 = $arr_font[2] - $arr_font[6]+20;
		$int_h1 = $arr_font[3] - $arr_font[7]+2;

		$arr_xy=self::get_watemark_pos($msg_pos,$int_w,$int_h,$int_w1,$int_h1);
		$int_x=$arr_xy["x"];
		$int_y=$arr_xy["y"];
		if($int_w<$int_w1||$int_h<$int_h1) return "目标图片小于水印图片，无法加水印！";
		$int_r = hexdec(substr($msg_color,1,2));
		$int_g = hexdec(substr($msg_color,3,2));
		$int_b = hexdec(substr($msg_color,5));
		//imagestring($obj_img,$msg_font,$int_x,$int_y,$msg_text,imagecolorallocate($obj_img,$int_r,$int_g,$int_b));

		$pic_themp=imagecreate($int_w1,$int_h1);
		$font_bg=imagecolorallocate($pic_themp,255,255,255);
		$font_color=imagecolorallocate($pic_themp,$int_r,$int_g,$int_b);
		imagettftext($pic_themp,$msg_font_size,0,10,$msg_font_size+2,$font_color,$font_path,$font_text);

		switch($pic_info[2])
		{
			case 1 :
			{
				$im=imagecreatefromgif($msg_source);
				imagecopymerge($im,$pic_themp,$int_x,$int_y,0,0,$int_w1,$int_h1,50);
				imagegif($im, $str_target);
				break;
			}
			case 2 :
			{
				$im=imagecreatefromjpeg($msg_source);
				imagecopymerge($im,$pic_themp,$int_x,$int_y,0,0,$int_w1,$int_h1,50);
				imagejpeg($im, $str_target, $msg_quality);
				break;
			}
			case 3 :
			{
				$im=imagecreatefrompng($msg_source);
				imagecopymerge($im,$pic_themp,$int_x,$int_y,0,0,$int_w1,$int_h1,50);
				imagepng($im, $str_target);
				break;
			}
			default :
				return;
		}
	}
	function on_pic_watermark($msg_pic,$msg_im,$msg_pos=0,$msg_quality=100,$msg_target="")
	{
		if(!is_file($msg_pic)) return "图片不存在！";
		if(!is_file($msg_im)) return "水印图片不存在！";
		if(!is_pic($msg_pic)) return "只有图片才能加水印！";
		if(!is_pic($msg_im)) return "水印图片不存在！";
		($msg_target=="")? $str_target=$msg_pic:$str_target=$msg_target;
		$pic_info=getimagesize($msg_pic);
		$int_w=$pic_info[0];
		$int_h=$pic_info[1];
		$im_info=getimagesize($msg_im);
		$int_w1=$im_info[0];
		$int_h1=$im_info[1];
		if($int_w<$int_w1||$int_h<$int_h1) return "目标图片小于水印图片，无法加水印！";
		switch($im_info[2])
		{
			case 1 :
				$obj_im = imagecreatefromgif($msg_im);
				break;
			case 2 :
				$obj_im = imagecreatefromjpeg($msg_im);
				break;
			case 3 :
				$obj_im = imagecreatefrompng($msg_im);
				break;
			default :
				return "图片格式不支持！";
		}
		$arr_xy=self::get_watemark_pos($msg_pos,$int_w,$int_h,$int_w1,$int_h1);
		$int_x=$arr_xy["x"];
		$int_y=$arr_xy["y"];
		switch($pic_info[2])
		{
			case 1 :
			{
				$obj_pic = imagecreatefromgif($msg_pic);
				imagecopymerge($obj_pic,$obj_im,$int_x,$int_y,0,0,$int_w1,$int_h1,100);
				imagegif($obj_pic, $str_target);
				break;
			}
			case 2 :
			{
				$obj_pic = imagecreatefromjpeg($msg_pic);
				imagecopymerge($obj_pic,$obj_im,$int_x,$int_y,0,0,$int_w1,$int_h1,100);
				imagejpeg($obj_pic, $str_target, $msg_quality);
				break;
			}
			case 3 :
			{
				$obj_pic = imagecreatefrompng($msg_pic);
				imagecopymerge($obj_pic,$obj_im,$int_x,$int_y,0,0,$int_w1,$int_h1,100);
				imagepng($obj_pic, $str_target);
				break;
			}
			default :
				return;
		}
	}

}
?>