Commit 03567c07 authored by AI-甘富林's avatar AI-甘富林

图片上传bug

parent 70905178
......@@ -7171,9 +7171,9 @@
}
},
"node_modules/semver": {
"version": "7.7.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz",
"integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==",
"version": "7.8.4",
"resolved": "https://registry.npmmirror.com/semver/-/semver-7.8.4.tgz",
"integrity": "sha512-rUCObTnP32Q08R2uuIrt7r9PlEonuTmtuXYcW6s5kjdlj3xbnwe+21yXptAUYcMAABLkYYTtnmzb3w3EDZfueA==",
"dev": true,
"license": "ISC",
"bin": {
......
......@@ -179,6 +179,16 @@ export function ProductImageUpload({
processedFile = await compressImage(file, 1);
}
// 压缩后二次校验:Canvas 重新编码无损格式(PNG 等)可能反而变大
if (processedFile.size > 5 * 1024 * 1024) {
toast({
title: "错误",
description: `图片 ${file.name} 压缩后仍超过 5MB,请尝试缩小图片尺寸`,
variant: "destructive",
});
continue;
}
const fileExt = processedFile.name.split(".").pop();
const fileName = `product_${Date.now()}_${i}.${fileExt}`;
const filePath = `products/${fileName}`;
......
......@@ -43,11 +43,9 @@ export const compressImage = async (
// 绘制图片
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// 对于 AVIF 或 WebP 等格式,转换为 JPEG 以确保兼容性
// canvas.toBlob 在某些浏览器中可能不支持 AVIF 编码
const outputType = (file.type === 'image/avif' || file.type === 'image/webp')
? 'image/jpeg'
: file.type;
// 统一输出 JPEG:canvas.toBlob 的 quality 参数仅对 image/jpeg 有效
// PNG 等无损格式通过 Canvas 重新编码后可能反而变大,导致突破 bucket 大小限制
const outputType = 'image/jpeg';
// 生成新的文件名(如果格式改变)
const getNewFileName = (originalName: string, newType: string) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment