作者 lyh

gx脚本demo

@@ -328,15 +328,9 @@ class LoginController extends BaseController @@ -328,15 +328,9 @@ class LoginController extends BaseController
328 public function ceshi() 328 public function ceshi()
329 { 329 {
330 $cos = new CosService(); 330 $cos = new CosService();
331 - $cdnUrl = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com/upload/p/2000/image_product/2024-07/aero-y50-introduction-3.jpg'; 331 + $cdnUrl = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com/upload/p/1/image_other/2023-11/655d9c70b692e10129.png';
332 $data = [ 332 $data = [
333 - 'characters' => '这是水印',  
334 - 'position' => 'gravity/NorthWest',  
335 - 'dx' => 'dx/20',  
336 - 'dy' => 'dy/20',  
337 - 'font' => 'font/5b6u6L2v6buR', // 黑体  
338 - 'fontsize' => 'fontsize/30',  
339 - 'fill' => 'fill/I0RGRkZGRg==', // 白色 333 + 'text' => '这是水印',
340 ]; 334 ];
341 return $cos->addFieldImage($cdnUrl, $data); 335 return $cos->addFieldImage($cdnUrl, $data);
342 } 336 }
@@ -22,7 +22,7 @@ class CosService @@ -22,7 +22,7 @@ class CosService
22 * @method :post 22 * @method :post
23 * @time :2023/7/19 15:28 23 * @time :2023/7/19 15:28
24 */ 24 */
25 - public function uploadFile(&$files,$path,$filename, $binary = false) 25 + public function uploadFile(&$files,$path,$filename, $binary = false,$watermarkOptions = null)
26 { 26 {
27 $cos = config('filesystems.disks.cos'); 27 $cos = config('filesystems.disks.cos');
28 $cosClient = new Client([ 28 $cosClient = new Client([
@@ -34,11 +34,25 @@ class CosService @@ -34,11 +34,25 @@ class CosService
34 ]); 34 ]);
35 $key = $path.'/'.$filename; 35 $key = $path.'/'.$filename;
36 $Body = $binary ? $files : fopen($files->getRealPath(), 'r'); 36 $Body = $binary ? $files : fopen($files->getRealPath(), 'r');
37 - $cosClient->putObject([ 37 + $options = [
38 'Bucket' => $cos['bucket'], 38 'Bucket' => $cos['bucket'],
39 'Key' => $key, 39 'Key' => $key,
40 'Body' => $Body, 40 'Body' => $Body,
41 - ]); 41 + ];
  42 + //水印
  43 + if ($watermarkOptions) {
  44 + $options['Pic-Operations'] = json_encode([
  45 + 'is_pic_info' => 1,
  46 + 'rules' => [
  47 + [
  48 + 'fileid' => $filename, // 使用相同的文件名保存
  49 + 'rule' => $watermarkOptions,
  50 + ]
  51 + ]
  52 + ]);
  53 + }
  54 + // 上传文件
  55 + $cosClient->putObject($options);
42 return $key; 56 return $key;
43 } 57 }
44 58
@@ -169,28 +183,54 @@ class CosService @@ -169,28 +183,54 @@ class CosService
169 SouthEast:右下角 183 SouthEast:右下角
170 */ 184 */
171 public function addFieldImage($cdnUrl = '',$data = [],$is_image = false){ 185 public function addFieldImage($cdnUrl = '',$data = [],$is_image = false){
  186 + $domain = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com';//cos域名
  187 + $url = $domain . $cdnUrl;
172 if($is_image){ 188 if($is_image){
  189 + $data['image'] = $domain . $data['image'];
173 $param = [ 190 $param = [
174 - 'image/'.$this->urlSafeBase64Encode($data['image']),//文字水印名称  
175 - $data['position'] ?? 'gravity/SouthEast',  
176 - $data['dx'] ?? 'dx/10/dy/10',  
177 - $data['font'] ?? 'font/5bCP6aOe',//默认宋体  
178 - $data['fontsize'] ?? 'fontsize/24',//大小  
179 - $data['fill'] ?? 'fill/I0ZGRkZGRg==',//颜色 191 + 'image/'.$this->urlSafeBase64Encode($data['image'] ?? ''),//文字水印名称
  192 + 'gravity/'.$this->urlSafeBase64Encode($data['gravity'] ?? 'SouthEast'),
  193 + 'dx/'.$data['dx'] ?? 10,
  194 + 'dy/'. $data['dy'] ?? 10,
  195 + 'batch/'.$data['batch'] ?? 0,//平铺水印功能
  196 + 'dissolve/'.$data['dissolve'] ?? 50,//透明度
  197 + 'degree/'.$data['degree'] ?? 0,//文字水印的旋转角度设置,取值范围为0 - 360,默认0
180 ]; 198 ];
181 - $cdnUrl = $cdnUrl.'?imageMogr2/watermark/1/'.implode('/',$param); 199 + $url = $url.'?watermark/1/'.implode('/',$param);
182 }else{ 200 }else{
183 $param = [ 201 $param = [
184 - 'text/'.$this->urlSafeBase64Encode($data['characters']),//文字水印名称  
185 - $data['position'] ?? 'gravity/SouthEast',  
186 - $data['dx'] ?? 'dx/10/dy/10',  
187 - $data['font'] ?? 'font/5bCP6aOe',//默认宋体  
188 - $data['fontsize'] ?? 'fontsize/24',//大小  
189 - $data['fill'] ?? 'fill/I0ZGRkZGRg==',//颜色 202 + 'text/'.$this->urlSafeBase64Encode($data['text'] ?? ''),//文字水印名称
  203 + 'gravity/'.$this->urlSafeBase64Encode($data['gravity'] ?? 'SouthEast'),
  204 + 'dx/'.$data['dx'] ?? 10,
  205 + 'dy/'. $data['dy'] ?? 10,
  206 + 'font/'.$this->urlSafeBase64Encode($data['font'] ?? 'tahoma.ttf'),//默认宋体
  207 + $data['fontsize'] ?? 'fontsize/24',//水印文字字体大小,单位为磅,缺省值13
  208 + 'fill/'.$this->urlSafeBase64Encode($data['fill'] ?? '#3D3D3D'),//颜色
  209 + 'dissolve/'.$data['dissolve'] ?? 50,//透明度
  210 + 'degree/'.$data['degree'] ?? 0,//文字水印的旋转角度设置,取值范围为0 - 360,默认0
  211 + 'batch/'.$data['batch'] ?? 0,//平铺水印功能
  212 + 'shadow/'.$data['shadow'] ?? 0,//文字阴影效果,有效值为[0,100],默认为0,表示无阴影
190 ]; 213 ];
191 - $cdnUrl = $cdnUrl.'?watermark/2/'.implode('/',$param); 214 + $url = $url.'?watermark/2/'.implode('/',$param);
192 } 215 }
193 - return $cdnUrl; 216 + // 获取水印后的图片内容
  217 +// $imageContent = file_get_contents($url);
  218 +// // 使用 COS SDK 将图片重新上传并覆盖原图
  219 +// $cos = config('filesystems.disks.cos');
  220 +// $cosClient = new \Qcloud\Cos\Client([
  221 +// 'region' => $cos['region'],
  222 +// 'credentials' => [
  223 +// 'secretId' => $cos['credentials']['secretId'],
  224 +// 'secretKey' => $cos['credentials']['secretKey'],
  225 +// ],
  226 +// ]);
  227 +// // 上传并覆盖原图
  228 +// $cosClient->putObject([
  229 +// 'Bucket' => $cos['bucket'],
  230 +// 'Key' => $cdnUrl, // 去掉域名部分,得到存储桶内的路径
  231 +// 'Body' => $imageContent,
  232 +// ]);
  233 + return $url;
194 } 234 }
195 235
196 /** 236 /**
@@ -200,7 +240,10 @@ class CosService @@ -200,7 +240,10 @@ class CosService
200 * @method :post 240 * @method :post
201 * @time :2024/8/19 14:21 241 * @time :2024/8/19 14:21
202 */ 242 */
203 - public function urlSafeBase64Encode($data) { 243 + public function urlSafeBase64Encode($data = '') {
  244 + if(empty($data)){
  245 + return $data;
  246 + }
204 // 1. 使用标准的 BASE64 编码 247 // 1. 使用标准的 BASE64 编码
205 $base64 = base64_encode($data); 248 $base64 = base64_encode($data);
206 // 2. 将加号(+)替换成连接号(-) 249 // 2. 将加号(+)替换成连接号(-)
@@ -211,4 +254,50 @@ class CosService @@ -211,4 +254,50 @@ class CosService
211 $base64 = rtrim($base64, '='); 254 $base64 = rtrim($base64, '=');
212 return $base64; 255 return $base64;
213 } 256 }
  257 +
  258 + /**
  259 + * @remark :处理9宫格数据
  260 + * @name :getPosition
  261 + * @author :lyh
  262 + * @method :post
  263 + * @time :2024/8/19 15:16
  264 + */
  265 + public function getPosition(){
  266 + return [
  267 + 1=>'gravity/NorthWest',
  268 + 2=>'gravity/North',
  269 + 3=>'gravity/NorthEast',
  270 + 4=>'gravity/West',
  271 + 5=>'gravity/Center',
  272 + 6=>'gravity/East',
  273 + 7=>'gravity/SouthWest',
  274 + 8=>'gravity/South',
  275 + 9=>'gravity/SouthEast',
  276 + ];
  277 + }
  278 +
  279 + /**
  280 + * @remark :字体
  281 + * @name :getFont
  282 + * @author :lyh
  283 + * @method :post
  284 + * @time :2024/8/19 15:47
  285 + */
  286 + public function getFont(){
  287 + return [
  288 + 'simfang仿宋.ttf',
  289 + 'tahoma.ttf',
  290 + 'simhei黑体.ttf',
  291 + 'simkai楷体.ttf',
  292 + 'simsun宋体.ttc',
  293 + 'STHeiti Light华文黑体.ttc',
  294 + 'STHeiti Medium.ttc',
  295 + '幼圆.TTF',
  296 + 'ahronbd.ttf',
  297 + 'arial.ttf',
  298 + 'ariblk.ttf',
  299 + 'Helvetica.dfont',
  300 + 'HelveticaNeue.dfont'
  301 + ];
  302 + }
214 } 303 }