上传
This commit is contained in:
@@ -150,14 +150,20 @@ class UploadController
|
||||
$file->move($tmpPath);
|
||||
|
||||
// 压缩处理
|
||||
$finalPath = $this->compressImage($tmpPath, $mime);
|
||||
$finalPath = $this->compressImage($tmpPath, $file->getUploadName(), $mime);
|
||||
|
||||
$result = Storage::adapter('oss')
|
||||
->path(config('oss.oss_main_dir') . $sceneUrl . date('Ymd'))
|
||||
->size(1024*1024*30)
|
||||
// ->extYes(['image/jpeg','image/png'])
|
||||
// ->extNo(['image/gif'])
|
||||
->uploadFile($finalPath);
|
||||
// $result = Storage::adapter('oss')
|
||||
// ->path(config('oss.oss_main_dir') . $sceneUrl . date('Ymd'))
|
||||
// ->size(1024*1024*30)
|
||||
//// ->extYes(['image/jpeg','image/png'])
|
||||
//// ->extNo(['image/gif'])
|
||||
// ->uploadFile($finalPath);
|
||||
|
||||
$param = [
|
||||
'scene' => $scene,
|
||||
'path' => $finalPath
|
||||
];
|
||||
$result = (new UploadService())->uploadLocalFile($param);
|
||||
|
||||
// 清理临时文件
|
||||
@unlink($tmpPath);
|
||||
@@ -165,12 +171,12 @@ class UploadController
|
||||
@unlink($finalPath);
|
||||
}
|
||||
|
||||
$ossservice = new OssService();
|
||||
//$ossservice = new OssService();
|
||||
|
||||
$data = [
|
||||
'href' => $result->file_name,
|
||||
'href' => $result['file_name'],
|
||||
'alt' => '',
|
||||
'url' => $ossservice->generatePrivateUrl($result->file_name)
|
||||
'url' => $result['file_url']
|
||||
];
|
||||
|
||||
// 记录文件信息到数据库
|
||||
@@ -187,9 +193,8 @@ class UploadController
|
||||
}
|
||||
}
|
||||
|
||||
protected function compressImage(string $srcPath, string $mime): string
|
||||
protected function compressImage(string $srcPath, string $originalName, string $mime): string
|
||||
{
|
||||
// 不支持 imagick 直接返回原图
|
||||
if (!extension_loaded('imagick')) {
|
||||
return $srcPath;
|
||||
}
|
||||
@@ -204,21 +209,23 @@ class UploadController
|
||||
|
||||
$image->thumbnailImage($maxWidth, $maxHeight, true);
|
||||
|
||||
// 自动方向修正(拍照图必备)
|
||||
if (method_exists($image, 'autoOrient')) {
|
||||
$image->autoOrient();
|
||||
}
|
||||
|
||||
// PNG / JPEG → WebP
|
||||
// 根据 MIME 决定输出格式
|
||||
if (in_array($mime, ['image/png', 'image/jpeg'], true)) {
|
||||
$image->setImageFormat('webp');
|
||||
$image->setImageCompressionQuality(80);
|
||||
|
||||
$dstPath = $srcPath . '.webp';
|
||||
// 保留原文件名 + 新扩展名
|
||||
$pathInfo = pathinfo($originalName);
|
||||
$dstName = $pathInfo['filename'] . '.webp';
|
||||
$dstPath = dirname($srcPath) . '/' . $dstName;
|
||||
|
||||
$image->writeImage($dstPath);
|
||||
} else {
|
||||
// 其他类型直接原样
|
||||
$dstPath = $srcPath;
|
||||
$dstPath = $srcPath; // GIF 或其他类型直接原样
|
||||
}
|
||||
|
||||
$image->clear();
|
||||
@@ -227,7 +234,6 @@ class UploadController
|
||||
return $dstPath;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// 压缩失败直接返回原图
|
||||
return $srcPath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user