|
@@ -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
|
|
|
@@ -148,4 +162,152 @@ class CosService |
|
@@ -148,4 +162,152 @@ class CosService |
|
148
|
'mime' => $file->getMimeType(),
|
162
|
'mime' => $file->getMimeType(),
|
|
149
|
];
|
163
|
];
|
|
150
|
}
|
164
|
}
|
|
|
|
165
|
+
|
|
|
|
166
|
+
|
|
|
|
167
|
+ /**
|
|
|
|
168
|
+ * @remark :生成带水印的图片文件
|
|
|
|
169
|
+ * @name :addFieldImage
|
|
|
|
170
|
+ * @author :lyh
|
|
|
|
171
|
+ * @method :post
|
|
|
|
172
|
+ * @time :2024/8/19 11:01
|
|
|
|
173
|
+ * gravity/SouthEast:
|
|
|
|
174
|
+ gravity:表示水印的对齐方式。常见的值有:
|
|
|
|
175
|
+ NorthWest:左上角
|
|
|
|
176
|
+ North:顶部中间
|
|
|
|
177
|
+ NorthEast:右上角
|
|
|
|
178
|
+ West:左侧中间
|
|
|
|
179
|
+ Center:中心
|
|
|
|
180
|
+ East:右侧中间
|
|
|
|
181
|
+ SouthWest:左下角
|
|
|
|
182
|
+ South:底部中间
|
|
|
|
183
|
+ SouthEast:右下角
|
|
|
|
184
|
+ */
|
|
|
|
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;
|
|
|
|
188
|
+ if($is_image){
|
|
|
|
189
|
+ $param = [
|
|
|
|
190
|
+ 'image/'.$this->urlSafeBase64Encode($domain.$data['image'] ?? ''),//文字水印名称
|
|
|
|
191
|
+ 'gravity/'.($data['gravity'] ?? 'SouthEast'),
|
|
|
|
192
|
+ 'dx/'.($data['dx'] ?? 0),
|
|
|
|
193
|
+ 'dy/'. ($data['dy'] ?? 0),
|
|
|
|
194
|
+ 'batch/'.($data['batch'] ?? 0),//平铺水印功能
|
|
|
|
195
|
+ 'dissolve/'.($data['dissolve'] ?? 50),//透明度
|
|
|
|
196
|
+ 'degree/'.($data['degree'] ?? 0),//文字水印的旋转角度设置,取值范围为0 - 360,默认0
|
|
|
|
197
|
+ ];
|
|
|
|
198
|
+ $url = $url.'?watermark/1/'.implode('/',$param);
|
|
|
|
199
|
+ }else{
|
|
|
|
200
|
+ $param = [
|
|
|
|
201
|
+ 'text/'.$this->urlSafeBase64Encode($data['text'] ?? ''),//文字水印名称
|
|
|
|
202
|
+ 'gravity/'.($data['gravity'] ?? 'SouthEast'),
|
|
|
|
203
|
+ 'dx/'.($data['dx'] ?? 10),
|
|
|
|
204
|
+ 'dy/'. ($data['dy'] ?? 10),
|
|
|
|
205
|
+ 'font/'.$this->urlSafeBase64Encode($data['font'] ?? 'tahoma.ttf'),//默认宋体
|
|
|
|
206
|
+ 'fontsize/'.($data['fontsize'] ?? 24),//水印文字字体大小,单位为磅,缺省值13
|
|
|
|
207
|
+ 'fill/'.$this->urlSafeBase64Encode($data['fill'] ?? '#3D3D3D'),//颜色
|
|
|
|
208
|
+ 'dissolve/'.($data['dissolve'] ?? 50),//透明度
|
|
|
|
209
|
+ 'degree/'.($data['degree'] ?? 0),//文字水印的旋转角度设置,取值范围为0 - 360,默认0
|
|
|
|
210
|
+ 'batch/'.($data['batch'] ?? 0),//平铺水印功能
|
|
|
|
211
|
+ 'shadow/'.($data['shadow'] ?? 0),//文字阴影效果,有效值为[0,100],默认为0,表示无阴影
|
|
|
|
212
|
+ ];
|
|
|
|
213
|
+ $url = $url.'?watermark/2/'.implode('/',$param);
|
|
|
|
214
|
+ }
|
|
|
|
215
|
+ return $url;
|
|
|
|
216
|
+ }
|
|
|
|
217
|
+
|
|
|
|
218
|
+ /**
|
|
|
|
219
|
+ * @remark :添加水印后保存图片(覆盖)
|
|
|
|
220
|
+ * @name :uploadImages
|
|
|
|
221
|
+ * @author :lyh
|
|
|
|
222
|
+ * @method :post
|
|
|
|
223
|
+ * @time :2024/8/19 17:06
|
|
|
|
224
|
+ */
|
|
|
|
225
|
+ public function uploadImages($url,$cdnUrl){
|
|
|
|
226
|
+ // 获取水印后的图片内容
|
|
|
|
227
|
+ $imageContent = file_get_contents($url);
|
|
|
|
228
|
+ // 使用 COS SDK 将图片重新上传并覆盖原图
|
|
|
|
229
|
+ $cos = config('filesystems.disks.cos');
|
|
|
|
230
|
+ $cosClient = new Client([
|
|
|
|
231
|
+ 'region' => $cos['region'],
|
|
|
|
232
|
+ 'credentials' => [
|
|
|
|
233
|
+ 'secretId' => $cos['credentials']['secretId'],
|
|
|
|
234
|
+ 'secretKey' => $cos['credentials']['secretKey'],
|
|
|
|
235
|
+ ],
|
|
|
|
236
|
+ ]);
|
|
|
|
237
|
+ // 上传并覆盖原图
|
|
|
|
238
|
+ $cosClient->putObject([
|
|
|
|
239
|
+ 'Bucket' => $cos['bucket'],
|
|
|
|
240
|
+ 'Key' => $cdnUrl, // 去掉域名部分,得到存储桶内的路径
|
|
|
|
241
|
+ 'Body' => $imageContent,
|
|
|
|
242
|
+ ]);
|
|
|
|
243
|
+ return $cos['cdn'].$cdnUrl;
|
|
|
|
244
|
+ }
|
|
|
|
245
|
+
|
|
|
|
246
|
+ /**
|
|
|
|
247
|
+ * @remark :腾讯云安全的base64
|
|
|
|
248
|
+ * @name :urlSafeBase64Encode
|
|
|
|
249
|
+ * @author :lyh
|
|
|
|
250
|
+ * @method :post
|
|
|
|
251
|
+ * @time :2024/8/19 14:21
|
|
|
|
252
|
+ */
|
|
|
|
253
|
+ public function urlSafeBase64Encode($data = '') {
|
|
|
|
254
|
+ if(empty($data)){
|
|
|
|
255
|
+ return $data;
|
|
|
|
256
|
+ }
|
|
|
|
257
|
+ // 1. 使用标准的 BASE64 编码
|
|
|
|
258
|
+ $base64 = base64_encode($data);
|
|
|
|
259
|
+ // 2. 将加号(+)替换成连接号(-)
|
|
|
|
260
|
+ $base64 = str_replace('+', '-', $base64);
|
|
|
|
261
|
+ // 3. 将正斜线(/)替换成下划线(_)
|
|
|
|
262
|
+ $base64 = str_replace('/', '_', $base64);
|
|
|
|
263
|
+ // 4. 去掉末尾的等号(=)
|
|
|
|
264
|
+ $base64 = rtrim($base64, '=');
|
|
|
|
265
|
+ return $base64;
|
|
|
|
266
|
+ }
|
|
|
|
267
|
+
|
|
|
|
268
|
+ /**
|
|
|
|
269
|
+ * @remark :处理9宫格数据
|
|
|
|
270
|
+ * @name :getPosition
|
|
|
|
271
|
+ * @author :lyh
|
|
|
|
272
|
+ * @method :post
|
|
|
|
273
|
+ * @time :2024/8/19 15:16
|
|
|
|
274
|
+ */
|
|
|
|
275
|
+ public function getPosition(){
|
|
|
|
276
|
+ return [
|
|
|
|
277
|
+ 1=>'gravity/northWest',
|
|
|
|
278
|
+ 2=>'gravity/north',
|
|
|
|
279
|
+ 3=>'gravity/northEast',
|
|
|
|
280
|
+ 4=>'gravity/west',
|
|
|
|
281
|
+ 5=>'gravity/center',
|
|
|
|
282
|
+ 6=>'gravity/east',
|
|
|
|
283
|
+ 7=>'gravity/southWest',
|
|
|
|
284
|
+ 8=>'gravity/south',
|
|
|
|
285
|
+ 9=>'gravity/southEast',
|
|
|
|
286
|
+ ];
|
|
|
|
287
|
+ }
|
|
|
|
288
|
+
|
|
|
|
289
|
+ /**
|
|
|
|
290
|
+ * @remark :字体
|
|
|
|
291
|
+ * @name :getFont
|
|
|
|
292
|
+ * @author :lyh
|
|
|
|
293
|
+ * @method :post
|
|
|
|
294
|
+ * @time :2024/8/19 15:47
|
|
|
|
295
|
+ */
|
|
|
|
296
|
+ public function getFont(){
|
|
|
|
297
|
+ return [
|
|
|
|
298
|
+ 'simfang仿宋.ttf',
|
|
|
|
299
|
+ 'tahoma.ttf',
|
|
|
|
300
|
+ 'simhei黑体.ttf',
|
|
|
|
301
|
+ 'simkai楷体.ttf',
|
|
|
|
302
|
+ 'simsun宋体.ttc',
|
|
|
|
303
|
+ 'STHeiti Light华文黑体.ttc',
|
|
|
|
304
|
+ 'STHeiti Medium.ttc',
|
|
|
|
305
|
+ '幼圆.TTF',
|
|
|
|
306
|
+ 'ahronbd.ttf',
|
|
|
|
307
|
+ 'arial.ttf',
|
|
|
|
308
|
+ 'ariblk.ttf',
|
|
|
|
309
|
+ 'Helvetica.dfont',
|
|
|
|
310
|
+ 'HelveticaNeue.dfont'
|
|
|
|
311
|
+ ];
|
|
|
|
312
|
+ }
|
|
151
|
} |
313
|
} |