public function water($filename,$water,$pos=0,$pct=80){
// 加載水印圖片
$info = $this->getImageInfo($water);
if(!empty($info[0])){
$water_w = $info[0];
$water_h = $info[1];
$type = $info['type'];
$fun = 'imagecreatefrom'.$type;
$waterimg = $fun($water);
} else{
return false;
}
// 加載背景圖片
$info = $this->getImageInfo($this->img);
if(!empty($info[0])){
$old_w = $info[0];
$old_h = $info[1];
$type = $info['type'];
$fun = 'imagecreatefrom'.$type;
$oldimg = $fun($this->img);
} else{
return false;
}
// 剪切水印
$water_w >$old_w && $water_w = $old_w;
$water_h >$old_h && $water_h = $old_h;
// 水印位置
switch($pos){
case 0://隨機
$posX = rand(0,($old_w - $water_w));
$posY = rand(0,($old_h - $water_h));
break;
case 1://1為頂端居左
$posX = 0;
$posY = 0;
break;
case 2://2為頂端居中
$posX = ($old_w - $water_w) / 2;
$posY = 0;
break;
case 3://3為頂端居右
$posX = $old_w - $water_w;
$posY = 0;
break;
case 4://4為中部居左
$posX = 0;
$posY = ($old_h - $water_h) / 2;
break;
case 5://5為中部居中
$posX = ($old_w - $water_w) / 2;
$posY = ($old_h - $water_h) / 2;
break;
case 6://6為中部居右
$posX = $old_w - $water_w;
$posY = ($old_h - $water_h) / 2;
break;
case 7://7為底端居左
$posX = 0;
$posY = $old_h - $water_h;
break;
case 8://8為底端居中
$posX = ($old_w - $water_w) / 2;
$posY = $old_h - $water_h;
break;
case 9://9為底端居右
$posX = $old_w - $water_w;
$posY = $old_h - $water_h;
break;
default: //隨機
$posX = rand(0,($old_w - $water_w));
$posY = rand(0,($old_h - $water_h));
break;
}
// 設定圖像的混色模式
imagealphablending($oldimg, true);
// 添加水印
imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);
$fun = 'image'.$type;
!@$fun($oldimg, $filename,100) && die('保存失敗!檢查目錄是否存在並且可寫?');
imagedestroy($oldimg);
imagedestroy($waterimg);
return $filename;
}