目标:1、主:一张图片添加多个水印 ok
2、副:水印图片可缩放、旋转 no 3、加完水印可压缩 okgetImgInfo($bpath); $this->bpath = $bpath; $this->bw = $bres['w']; $this->bh = $bres['h']; $this->btype = $bres['type']; } /** * 根据图片类型创建对象 * @param string 图片类型 * @return obj */ public function createObj($path) { $info = $this->getImgInfo($path); $type = $info['type']; $func = 'imagecreatefrom'.$type; return $func($path); } /** * 获取图片尺寸 * @param [type] $path [description] * @return [type] [w:宽, h:高,type :类型] */ public function getImgInfo($path) { $info = getimagesize($path); $res['w'] = $info[0]; $res['h'] = $info[1]; $res['type'] = image_type_to_extension($info[2],false); return $res; } /** * 模糊定位,获取水印图相对于背景图的 x,y * @return [type] [description] */ public function getPosition($wpath,$p ='') { $binfo = getimagesize($this->bpath); // echo ""; // 获取水印图信息 $waterinfo = getimagesize($wpath); /*原图必须大于水印*/ if($binfo[0] < $waterinfo[0]){ return ['error'=>'水印图过大']; } if($binfo[1] < $waterinfo[1]){ return ['error'=>'水印图过高']; } $positon = empty($p) ? $this->positon : $p; switch ($positon) { //1顶部居左 case 1: $x=$y=0; break; //2顶部居右 case 2: // 原图宽 - 水印图宽 $x = $binfo[0] - $waterinfo[0]; $y = 0; break; //3居中 case 3: $x = ($binfo[0] - $waterinfo[0])/2; $y = ($binfo[1] - $waterinfo[1])/2; break; //4底部居左 case 4: $x = 0; $y = $binfo[1] - $waterinfo[1]; break; //5底部居右 case 5: $x = $binfo[0] - $waterinfo[0]; $y = $binfo[1] - $waterinfo[1]; break; default: $x=$y=0; } return ['x'=>$x, 'y'=>$y, 'w'=>$waterinfo[0], 'h'=>$waterinfo[1], 'error'=>false ]; } /** * 创建单图片水印 * @param $wpath 水印图路径 * @return [type] [description] */ public function createWate($wpath) { // 创建图片对象 $bobj = $this->createObj($this->bpath); $wobj = $this->createObj($wpath); $pores = $this->getPosition($wpath); if($pores['error']){ return $pores['error']; } $x = $pores['x']; $y = $pores['y']; $w = $pores['w']; $h = $pores['h']; // 原图obj , 水印图obj ,水start_x , 水start_y, 水x , 水y ,拷贝的宽度, 拷贝的高度 , 透明度 $res = imagecopymerge($bobj, $wobj, $x, $y, 0, 0, $w, $h, $this->alpha); header('Content-Type: image/jpeg'); imagepng($bobj); imagedestroy($bobj); imagedestroy($wobj); } /** * 创建多个图片水印 * @param [array] $waterpaths [水印图路径] * @param [array] $positon [水印位置] * @param [array] $alpha [水印透明度] * @param [int] $re [缩放值 0 - 1] * @return [type] [description] * 使用方式 * $res =$obj->createWates([$waterpath,'./img/share.png'],[1,3],[40,50]); */ public function createWates($waterpaths = array(),$positon = array(),$alpha = array(),$percent = 1) { // 创建背景对象 $bobj = $this->createObj($this->bpath); $deobjs[] = $bobj; foreach ($waterpaths as $key => $value) { $deobjs[] = $nobj = $this->createObj($value); $pos = empty($positon[$key]) ? $this->positon : $positon[$key]; $al = empty($alpha[$key]) ? $this->alpha : $alpha[$key]; $pores = $this->getPosition($value,$pos); if($pores['error']){ return $pores['error']; } $x = $pores['x']; $y = $pores['y']; $w = $pores['w']; $h = $pores['h']; // 原图obj , 水印图obj ,水start_x , 水start_y, 水x , 水y ,拷贝的宽度, 拷贝的高度 , 透明度 imagecopymerge($bobj, $nobj, $x, $y, 0, 0, $w, $h, $al); } // 是否开启缩放 if( $percent < 1){ // 压缩 $this->nw = $this->bw * $percent; $this->nh = $this->bh * $percent; // 分配颜色 $newsouce = imagecreatetruecolor($this->nw, $this->nh); // 填充背景 $white = ImageColorAllocate($newsouce,255,255,255); // 开始缩放 imagecopyresampled($newsouce, $bobj, 0, 0, 0, 0, $this->nw, $this->nh, $this->bw, $this->bh); $bobj = $newsouce; } $t = $this->btype; header("Content-Type: image/$t"); $func = "image" . $t; $func($bobj); foreach ($deobjs as $v) { imagedestroy($v); } } /** * 创建缩放的图片 * @param 图片路径 * @return obj */ public function createReObj($filepath) { // 获得原来的图片大小 list($width, $height) = getimagesize($filepath); $this->nw = $width * $this->percent; $this->nh = $height * $this->percent; // 原比例 $ratio_orig = $width / $height; // 新比例 $neww = $this->nw / $this->nh; // 源图像资源 $reobj = $this->createObj($filepath); // 分配颜色 $image = imagecreatetruecolor($this->nw, $this->nh); // 填充背景 $white = ImageColorAllocate($image,255,255,255); // 开始缩放 imagecopyresampled($image, $reobj, 0, 0, 0, 0, $this->nw, $this->nh, $width, $height); imagepng($image); }}//多水印使用方式$bpath = './img/4.jpg';$waterpath = './img/tx.png';$obj = new WaterMark($bpath);// $obj->createWate($waterpath);$res = $obj->createWates([$waterpath,'./img/share.png'],[1,3],[40,50],0.5);