CosService.php
6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace App\Services;
use App\Exceptions\InquiryFilterException;
use App\Utils\LogUtils;
use Qcloud\Cos\Client;
/**
* @remark :对象存储cos
* @class :CosService.php
* @author :lyh
* @time :2023/7/19 15:09
*/
class CosService
{
/**
* @param $file
* @remark :上传图片
* @name :uploadFile
* @author :lyh
* @method :post
* @time :2023/7/19 15:28
*/
public function uploadFile(&$files,$path,$filename, $binary = false)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$key = $path.'/'.$filename;
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $key,
'Body' => $Body,
]);
return $key;
}
/**
* @param $image_name
* @remark :获取图片访问链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/19 16:08
*/
public function getImageUrl($image_name)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$imageUrl = $cosClient->getObjectUrl($cos['bucket'], trim($image_name,'/'), '+10 years');
return $imageUrl;
}
/**
* 根据远程图片地址上传
* @param $project_id
* @param $image_type
* @param $file_url
* @param $key
* @param $body_str
* @param int $same_name 是否保持名称一直
* @return string
* @author Akun
* @date 2023/09/21 9:39
*/
public static function uploadRemote($project_id,$image_type,$file_url,$key='',$body_str='',$same_name=0)
{
if(!$key){
$url_arr = parse_url($file_url);
if($same_name){
$path_arr = explode('/',$url_arr['path']);
$filename = end($path_arr);
}else{
$ext = explode('.',$url_arr['path']);
$filename = uniqid().rand(10000,99999).'.'.end($ext);
}
$uploads = config('upload.default_file');
$path = $uploads['path_b'].'/'.$project_id.'/'.$image_type.'/'.date('Y-m');
$key = $path.'/'.$filename;
}
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
try {
$body = $body_str ?:curl_c($file_url,false);
}catch (\Exception $e){
return '';
}
try {
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $key,
'Body' => $body,
]);
return $key;
}catch (\Exception $e){
LogUtils::error('uploadRemote error', $e->getMessage());
return '';
}
}
/**
* @param $file
* @return array
* @throws \Exception
* @author zbj
* @date 2023/12/12
*/
public function checkInquiryFile($file){
$size = $file->getSize();
if($size/1024/1024 > 20){
throw new InquiryFilterException('Your file size exceeds the limit. Please upload a file no larger than 20MB.');
}
$extension = $file->getClientOriginalExtension();
// JPEG (JPG) PDF DWG STEP(STP)IGS word xlsx
if(!in_array(strtolower($extension), ['png','jpg','jpeg', 'pdf', 'dwg', 'step', 'stp', 'igs','doc','docx','xls','xlsx'])){
throw new InquiryFilterException('Please upload file in png, jpg, jpeg, pdf, dwg, step, stp, igs, doc, docx, xls or xlsx format.');
}
return [
'size' => $size,
'extension' => $extension,
'name' => $file->getClientOriginalName(),
'mime' => $file->getMimeType(),
];
}
/**
* @remark :生成带水印的图片文件
* @name :addFieldImage
* @author :lyh
* @method :post
* @time :2024/8/19 11:01
* gravity/SouthEast:
gravity:表示水印的对齐方式。常见的值有:
NorthWest:左上角
North:顶部中间
NorthEast:右上角
West:左侧中间
Center:中心
East:右侧中间
SouthWest:左下角
South:底部中间
SouthEast:右下角
*/
public function addFieldImage($cdnUrl = '',$data = [],$is_image = false){
if($is_image){
$param = [
'image/'.urlSafeBase64Encode($data['image']),//文字水印名称
$data['position'] ?? 'gravity/SouthEast',
$data['dx'] ?? 'dx/10/dy/10',
$data['font'] ?? 'font/5bCP6aOe',//默认宋体
$data['fontsize'] ?? 'fontsize/24',//大小
$data['fill'] ?? 'fill/I0ZGRkZGRg==',//颜色
];
$cdnUrl = $cdnUrl.'?imageMogr2/watermark/1/'.implode('/',$param);
}else{
$param = [
'text/'.urlSafeBase64Encode($data['characters']),//文字水印名称
$data['position'] ?? 'gravity/SouthEast',
$data['dx'] ?? 'dx/10/dy/10',
$data['font'] ?? 'font/5bCP6aOe',//默认宋体
$data['fontsize'] ?? 'fontsize/24',//大小
$data['fill'] ?? 'fill/I0ZGRkZGRg==',//颜色
];
$cdnUrl = $cdnUrl.'?watermark/2/'.implode('/',$param);
}
return $cdnUrl;
}
/**
* @remark :腾讯云安全的base64
* @name :urlSafeBase64Encode
* @author :lyh
* @method :post
* @time :2024/8/19 14:21
*/
public function urlSafeBase64Encode($data) {
// 1. 使用标准的 BASE64 编码
$base64 = base64_encode($data);
// 2. 将加号(+)替换成连接号(-)
$base64 = str_replace('+', '-', $base64);
// 3. 将正斜线(/)替换成下划线(_)
$base64 = str_replace('/', '_', $base64);
// 4. 去掉末尾的等号(=)
$base64 = rtrim($base64, '=');
return $base64;
}
}