refactor event
This commit is contained in:
@@ -3,16 +3,31 @@ import imageCompression from "browser-image-compression";
|
||||
const PHOTO_MAX_SIZE_MB = 300 / 1024;
|
||||
const PHOTO_MAX_WIDTH_OR_HEIGHT = 1920;
|
||||
|
||||
/**
|
||||
* 转为真正的 File,避免压缩库返回 Blob 后 FormData 使用默认文件名 blob。
|
||||
*/
|
||||
function normalizeUploadImageFile(source: File, compressed: Blob): File {
|
||||
if (compressed instanceof File && compressed.name) {
|
||||
return compressed;
|
||||
}
|
||||
|
||||
return new File([compressed], source.name || "upload-image", {
|
||||
type: compressed.type || source.type,
|
||||
lastModified: source.lastModified
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩上传图片,统一限制到 300 KB。
|
||||
*/
|
||||
export async function compressUploadImage(file: File): Promise<File> {
|
||||
return await imageCompression(file, {
|
||||
const compressedFile = await imageCompression(file, {
|
||||
maxSizeMB: PHOTO_MAX_SIZE_MB,
|
||||
maxWidthOrHeight: PHOTO_MAX_WIDTH_OR_HEIGHT,
|
||||
useWebWorker: true,
|
||||
initialQuality: .86
|
||||
});
|
||||
return normalizeUploadImageFile(file, compressedFile);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user