6 Commits

Author SHA1 Message Date
18338781771 4ec46704b2 imagick 2026-01-15 19:09:35 +08:00
18338781771 80e1a170e5 上传 2026-01-15 18:44:07 +08:00
18338781771 f2196809ea 图片上传 2026-01-15 18:04:39 +08:00
18338781771 05675471ab 上传图片 2026-01-15 18:03:14 +08:00
18338781771 c77da51775 图片压缩 2026-01-15 17:55:51 +08:00
18338781771 3d51a841cf 上传 2026-01-15 17:30:11 +08:00
+58 -22
View File
@@ -135,7 +135,7 @@ class UploadController
'image/webp', 'image/webp',
]; ];
$mime = $file->getMimeType(); $mime = $file->getUploadMimeType();
if (!in_array($mime, $allowMime, true)) { if (!in_array($mime, $allowMime, true)) {
return $this->error('不支持的图片类型,支持jpeg、png、webp'); return $this->error('不支持的图片类型,支持jpeg、png、webp');
} }
@@ -146,18 +146,29 @@ class UploadController
mkdir($tmpDir, 0755, true); 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); $file->move($tmpPath);
// 压缩处理 // 压缩处理
$finalPath = $this->compressImage($tmpPath, $mime); $finalPath = $this->compressImage($tmpPath, $originalName, $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'])
->upload($file); // ->uploadFile($finalPath);
$param = [
'scene' => $scene,
'path' => $finalPath,
'original_name' => $originalName
];
$result = (new UploadService())->uploadLocalFile($param);
// 清理临时文件 // 清理临时文件
@unlink($tmpPath); @unlink($tmpPath);
@@ -165,12 +176,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,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')) { 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; return $srcPath;
} }
@@ -199,26 +222,28 @@ class UploadController
$image->readImage($srcPath); $image->readImage($srcPath);
// 防止超大图 // 防止超大图
$maxWidth = 2000; $maxWidth = 1280;
$maxHeight = 2000; $maxHeight = 1280;
$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 +252,18 @@ class UploadController
return $dstPath; return $dstPath;
} catch (\Throwable $e) { } 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; return $srcPath;
} }
} }