123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/3/12
- * Time: 上午9:57
- */
- namespace backend\server;
- use Imagine\Image\ManipulatorInterface;
- use yii\imagine\Image;
- use common\models\CategoryWatermark;
- use Yii;
- class SetUploadImg
- {
- // private $spacing = 20;
- // private $tmp;
- private $WatermarkInfo;
- public function __construct()
- {
- $model = new CategoryWatermark();
- $watermaekinfo = $model->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);
- }
- }
|