SetUploadImg.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/12
  6. * Time: 上午9:57
  7. */
  8. namespace backend\server;
  9. use Imagine\Image\ManipulatorInterface;
  10. use yii\imagine\Image;
  11. use common\models\CategoryWatermark;
  12. use Yii;
  13. class SetUploadImg
  14. {
  15. // private $spacing = 20;
  16. // private $tmp;
  17. private $WatermarkInfo;
  18. public function __construct()
  19. {
  20. $model = new CategoryWatermark();
  21. $watermaekinfo = $model->getWatermarkInfo();
  22. if($watermaekinfo == null) return false;
  23. $this->WatermarkInfo = $watermaekinfo;
  24. // $this->position = $watermaekinfo['position'];
  25. // if($watermaekinfo['spacing'] != null) $this->WatermarkInfo['spacing'] = $watermaekinfo['spacing'];
  26. // $this->tmp = Yii::$app->params['watermark_img']['url'].$watermaekinfo['template'];
  27. }
  28. public function SetImgWatermark($image,$path)
  29. {
  30. if(is_string($image))
  31. {
  32. $image = explode(',',$image);
  33. }
  34. $this->WatermarkInfo['template'] = Yii::$app->params['watermark_img']['url'].$this->WatermarkInfo['template'];
  35. foreach ($image as $val)
  36. {
  37. $sizeImage = getimagesize($path.$val);
  38. if(empty($sizeImage[0])) return false;
  39. if( $sizeImage[0] >= $this->WatermarkInfo['width'] && $sizeImage[1] >= $this->WatermarkInfo['height'] )
  40. {
  41. static::WatermarkHandle($path.$val);
  42. }
  43. // if($sizeImage[0] < $this->WatermarkInfo['width'] || $sizeImage[1] < $this->WatermarkInfo['height']) return false;
  44. }
  45. // $this->position = $watermaekinfo['position'];
  46. // if($watermaekinfo['spacing'] != null)
  47. // {
  48. // $this->WatermarkInfo['spacing'] = $watermaekinfo['spacing'];
  49. // }
  50. //
  51. // if()
  52. //
  53. // $this->WatermarkHandle($image,Yii::$app->params['watermark_img']['url'].$watermaekinfo['template']);
  54. }
  55. private function WatermarkHandle($img)
  56. {
  57. $sizeWatemark = getimagesize($this->WatermarkInfo['template']);
  58. $sizeImage = getimagesize($img);
  59. switch ($this->WatermarkInfo['position'])
  60. {
  61. case 1://左上
  62. $pos = [$this->WatermarkInfo['spacing'],$this->WatermarkInfo['spacing']];
  63. break;
  64. case 2: //顶部居中
  65. $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$this->WatermarkInfo['spacing']];
  66. break;
  67. case 3: //右上
  68. $pos = [$sizeImage[0]-($sizeWatemark[0]+$this->WatermarkInfo['spacing']),$this->WatermarkInfo['spacing']];
  69. break;
  70. case 4: //中间居左
  71. $pos = [$this->WatermarkInfo['spacing'],$sizeImage[1]/2 - $sizeWatemark[1]/2];
  72. break;
  73. case 5: //居中
  74. $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]/2 - $sizeWatemark[1]/2];
  75. break;
  76. case 6: //中间居右
  77. $pos = [$sizeImage[0] - ($sizeWatemark[0] + $this->WatermarkInfo['spacing']),$sizeImage[1]/2 - $sizeWatemark[1]/2];
  78. break;
  79. case 7://左下
  80. $pos = [$this->WatermarkInfo['spacing'],$sizeImage[1]-($sizeWatemark[1]+$this->WatermarkInfo['spacing'])];
  81. break;
  82. case 8: //底部居中
  83. $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]-($sizeWatemark[1] + $this->WatermarkInfo['spacing'])];
  84. break;
  85. case 9://右下
  86. $pos = [$sizeImage[0] - ($sizeWatemark[0] + $this->WatermarkInfo['spacing']),$sizeImage[1]-($sizeWatemark[1] + $this->WatermarkInfo['spacing'])];
  87. break;
  88. default:
  89. return false;
  90. // throw new \ErrorException('请选择水印位置');
  91. break;
  92. }
  93. Image::watermark($img,$this->WatermarkInfo['template'],$pos)
  94. ->save($img);
  95. }
  96. /*
  97. * 水印处理
  98. * @params $image 原图片
  99. * @params $watemarkImage 水印图
  100. * @params $position 水印的位置,1-9表示 废弃
  101. * @params $spacing 水印距离边界的间距
  102. * */
  103. public static function Watermark($image,$watemarkImage,$position,$spacing = 20)
  104. {
  105. $sizeImage = getimagesize($image);
  106. if(empty($sizeImage)) return false;
  107. if($sizeImage[0] < 600 || $sizeImage[1] < 560) return false;
  108. $sizeWatemark = getimagesize($watemarkImage);
  109. switch ($position)
  110. {
  111. case 1://左上
  112. $pos = [$spacing,$spacing];
  113. break;
  114. case 2: //顶部居中
  115. $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$spacing];
  116. break;
  117. case 3: //右上
  118. $pos = [$sizeImage[0]-($sizeWatemark[0]+$spacing),$spacing];
  119. break;
  120. case 4: //中间居左
  121. $pos = [$spacing,$sizeImage[1]/2 - $sizeWatemark[1]/2];
  122. break;
  123. case 5: //居中
  124. $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]/2 - $sizeWatemark[1]/2];
  125. break;
  126. case 6: //中间居右
  127. $pos = [$sizeImage[0] - ($sizeWatemark[0] + $spacing),$sizeImage[1]/2 - $sizeWatemark[1]/2];
  128. break;
  129. case 7://左下
  130. $pos = [$spacing,$sizeImage[1]-($sizeWatemark[1]+$spacing)];
  131. break;
  132. case 8: //底部居中
  133. $pos = [$sizeImage[0]/2 - $sizeWatemark[0]/2,$sizeImage[1]-($sizeWatemark[1] + $spacing)];
  134. break;
  135. case 9://右下
  136. $pos = [$sizeImage[0] - ($sizeWatemark[0] + $spacing),$sizeImage[1]-($sizeWatemark[1] + $spacing)];
  137. break;
  138. default:
  139. return false;
  140. // throw new \ErrorException('请选择水印位置');
  141. break;
  142. }
  143. Image::watermark($image,$watemarkImage,$pos)
  144. ->save($image);
  145. }
  146. }