Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ec46704b2 | |||
| 80e1a170e5 | |||
| f2196809ea | |||
| 05675471ab | |||
| c77da51775 | |||
| 3d51a841cf |
@@ -135,7 +135,7 @@ class UploadController
|
||||
'image/webp',
|
||||
];
|
||||
|
||||
$mime = $file->getMimeType();
|
||||
$mime = $file->getUploadMimeType();
|
||||
if (!in_array($mime, $allowMime, true)) {
|
||||
return $this->error('不支持的图片类型,支持jpeg、png、webp');
|
||||
}
|
||||
@@ -146,18 +146,29 @@ class UploadController
|
||||
mkdir($tmpDir, 0755, true);
|
||||
}
|
||||
|
||||
$tmpPath = $tmpDir . '/' . uniqid('img_', true);
|
||||
// 保留原始文件扩展名
|
||||
$originalName = $file->getUploadName();
|
||||
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
|
||||
$tmpFileName = uniqid('img_', true) . ($extension ? '.' . $extension : '');
|
||||
$tmpPath = $tmpDir . '/' . $tmpFileName;
|
||||
$file->move($tmpPath);
|
||||
|
||||
// 压缩处理
|
||||
$finalPath = $this->compressImage($tmpPath, $mime);
|
||||
$finalPath = $this->compressImage($tmpPath, $originalName, $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'])
|
||||
->upload($file);
|
||||
// $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,
|
||||
'original_name' => $originalName
|
||||
];
|
||||
$result = (new UploadService())->uploadLocalFile($param);
|
||||
|
||||
// 清理临时文件
|
||||
@unlink($tmpPath);
|
||||
@@ -165,12 +176,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,10 +198,22 @@ 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')) {
|
||||
// imagick 未安装时,确保返回的路径有扩展名
|
||||
$extension = pathinfo($srcPath, PATHINFO_EXTENSION);
|
||||
if (!$extension) {
|
||||
// 如果临时文件没有扩展名,从原始文件名获取
|
||||
$originalExtension = pathinfo($originalName, PATHINFO_EXTENSION);
|
||||
if ($originalExtension) {
|
||||
$newPath = $srcPath . '.' . $originalExtension;
|
||||
if (file_exists($srcPath)) {
|
||||
rename($srcPath, $newPath);
|
||||
return $newPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $srcPath;
|
||||
}
|
||||
|
||||
@@ -199,26 +222,28 @@ class UploadController
|
||||
$image->readImage($srcPath);
|
||||
|
||||
// 防止超大图
|
||||
$maxWidth = 2000;
|
||||
$maxHeight = 2000;
|
||||
$maxWidth = 1280;
|
||||
$maxHeight = 1280;
|
||||
|
||||
$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 +252,18 @@ class UploadController
|
||||
return $dstPath;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// 压缩失败直接返回原图
|
||||
// 压缩失败时,确保返回的路径有扩展名
|
||||
$extension = pathinfo($srcPath, PATHINFO_EXTENSION);
|
||||
if (!$extension) {
|
||||
$originalExtension = pathinfo($originalName, PATHINFO_EXTENSION);
|
||||
if ($originalExtension) {
|
||||
$newPath = $srcPath . '.' . $originalExtension;
|
||||
if (file_exists($srcPath)) {
|
||||
rename($srcPath, $newPath);
|
||||
return $newPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $srcPath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user