getWatermarkInfo(); if($watermaekinfo == null) return false; $this->WatermarkInfo = $watermaekinfo; // $this->position = $watermaekinfo['position']; // if($watermaekinfo['spacing'] != null) $this->WatermarkInfo['spacing'] = $watermaekinfo['spacing']; // $this->tmp = Yii::$app->params['watermark_img']['url'].$watermaekinfo['template']; } public function SetImgWatermark($image,$path) { if(is_string($image)) { $image = explode(',',$image); } $this->WatermarkInfo['template'] = Yii::$app->params['watermark_img']['url'].$this->WatermarkInfo['template']; foreach ($image as $val) { $sizeImage = getimagesize($path.$val); if(empty($sizeImage[0])) return false; if( $sizeImage[0] >= $this->WatermarkInfo['width'] && $sizeImage[1] >= $this->WatermarkInfo['height'] ) { static::WatermarkHandle($path.$val); } // if($sizeImage[0] < $this->WatermarkInfo['width'] || $sizeImage[1] < $this->WatermarkInfo['height']) return false; } // $this->position = $watermaekinfo['position']; // if($watermaekinfo['spacing'] != null) // { // $this->WatermarkInfo['spacing'] = $watermaekinfo['spacing']; // } // // if() // // $this->WatermarkHandle($image,Yii::$app->params['watermark_img']['url'].$watermaekinfo['template']); } private function WatermarkHandle($img) { $sizeWatemark = getimagesize($this->WatermarkInfo['template']); $sizeImage = getimagesize($img); switch ($this->WatermarkInfo['position']) { case 1://左上 $pos = [$this->WatermarkInfo['spacing'],$this->WatermarkInfo['spacing']]; break; case 2: //顶部居中 $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$this->WatermarkInfo['spacing']]; break; case 3: //右上 $pos = [$sizeImage[0]-($sizeWatemark[0]+$this->WatermarkInfo['spacing']),$this->WatermarkInfo['spacing']]; break; case 4: //中间居左 $pos = [$this->WatermarkInfo['spacing'],$sizeImage[1]/2 - $sizeWatemark[1]/2]; break; case 5: //居中 $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]/2 - $sizeWatemark[1]/2]; break; case 6: //中间居右 $pos = [$sizeImage[0] - ($sizeWatemark[0] + $this->WatermarkInfo['spacing']),$sizeImage[1]/2 - $sizeWatemark[1]/2]; break; case 7://左下 $pos = [$this->WatermarkInfo['spacing'],$sizeImage[1]-($sizeWatemark[1]+$this->WatermarkInfo['spacing'])]; break; case 8: //底部居中 $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]-($sizeWatemark[1] + $this->WatermarkInfo['spacing'])]; break; case 9://右下 $pos = [$sizeImage[0] - ($sizeWatemark[0] + $this->WatermarkInfo['spacing']),$sizeImage[1]-($sizeWatemark[1] + $this->WatermarkInfo['spacing'])]; break; default: return false; // throw new \ErrorException('请选择水印位置'); break; } Image::watermark($img,$this->WatermarkInfo['template'],$pos) ->save($img); } /* * 水印处理 * @params $image 原图片 * @params $watemarkImage 水印图 * @params $position 水印的位置,1-9表示 废弃 * @params $spacing 水印距离边界的间距 * */ public static function Watermark($image,$watemarkImage,$position,$spacing = 20) { $sizeImage = getimagesize($image); if(empty($sizeImage)) return false; if($sizeImage[0] < 600 || $sizeImage[1] < 560) return false; $sizeWatemark = getimagesize($watemarkImage); switch ($position) { case 1://左上 $pos = [$spacing,$spacing]; break; case 2: //顶部居中 $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$spacing]; break; case 3: //右上 $pos = [$sizeImage[0]-($sizeWatemark[0]+$spacing),$spacing]; break; case 4: //中间居左 $pos = [$spacing,$sizeImage[1]/2 - $sizeWatemark[1]/2]; break; case 5: //居中 $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]/2 - $sizeWatemark[1]/2]; break; case 6: //中间居右 $pos = [$sizeImage[0] - ($sizeWatemark[0] + $spacing),$sizeImage[1]/2 - $sizeWatemark[1]/2]; break; case 7://左下 $pos = [$spacing,$sizeImage[1]-($sizeWatemark[1]+$spacing)]; break; case 8: //底部居中 $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]-($sizeWatemark[1] + $spacing)]; break; case 9://右下 $pos = [$sizeImage[0] - ($sizeWatemark[0] + $spacing),$sizeImage[1]-($sizeWatemark[1] + $spacing)]; break; default: return false; // throw new \ErrorException('请选择水印位置'); break; } Image::watermark($image,$watemarkImage,$pos) ->save($image); } }