|
...
|
...
|
@@ -24,7 +24,7 @@ class CosService |
|
|
|
* @method :post
|
|
|
|
* @time :2023/7/19 15:28
|
|
|
|
*/
|
|
|
|
public function uploadFile(&$files,$path,$filename, $binary = false,$watermarkOptions = '')
|
|
|
|
public function uploadFile(&$files, $path, $filename, $binary = false, $watermarkOptions = '')
|
|
|
|
{
|
|
|
|
$cos = config('filesystems.disks.cos');
|
|
|
|
$cosClient = new Client([
|
|
...
|
...
|
@@ -34,7 +34,9 @@ class CosService |
|
|
|
'secretKey' => $cos['credentials']['secretKey'],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$key = $path.'/'.$filename;
|
|
|
|
|
|
|
|
$key = $path . '/' . $filename;
|
|
|
|
|
|
|
|
// 判断是否为 Base64 编码的图片流文件
|
|
|
|
if (Str::startsWith($files, 'data:image')) {
|
|
|
|
// 分离 Base64 头部和数据部分
|
|
...
|
...
|
@@ -42,11 +44,22 @@ class CosService |
|
|
|
// 解码 Base64 数据
|
|
|
|
$Body = base64_decode($base64Data);
|
|
|
|
if ($Body === false) {
|
|
|
|
throw new \Exception("Base64 数据解码失败");
|
|
|
|
outMessage('upload_images',"解码失败");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 如果不是 Base64 流文件,处理为普通文件上传
|
|
|
|
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
|
|
|
|
try {
|
|
|
|
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
|
|
|
|
// 检查文件是否有效
|
|
|
|
if (!$Body) {
|
|
|
|
outMessage('upload_images',"文件打开失败");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
outMessage('upload_images',"文件处理失败: " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$options = [
|
|
...
|
...
|
@@ -54,7 +67,7 @@ class CosService |
|
|
|
'Key' => $key,
|
|
|
|
'Body' => $Body,
|
|
|
|
];
|
|
|
|
//水印
|
|
|
|
// 水印处理
|
|
|
|
if ($watermarkOptions) {
|
|
|
|
$options['PicOperations'] = json_encode([
|
|
|
|
'is_pic_info' => 1,
|
|
...
|
...
|
@@ -66,15 +79,21 @@ class CosService |
|
|
|
]
|
|
|
|
], true);
|
|
|
|
}
|
|
|
|
$res = $cosClient->putObject($options);
|
|
|
|
@file_put_contents(storage_path('logs/upload_images'), var_export($res, true) . PHP_EOL, FILE_APPEND);
|
|
|
|
}catch (\Exception $e){
|
|
|
|
@file_put_contents(storage_path('logs/upload_images'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
|
|
|
|
$cosClient->putObject($options);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// 记录上传失败日志
|
|
|
|
outMessage('upload_images',$e->getMessage());
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
// 确保非二进制模式下关闭文件资源
|
|
|
|
if (!$binary && is_resource($Body)) {
|
|
|
|
fclose($Body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $image_name
|
|
|
|
* @remark :获取图片访问链接
|
...
|
...
|
|