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