260623_05_ENH: 이미지 압축 전송 적용
This commit is contained in:
101
src/lib/utils.ts
101
src/lib/utils.ts
@@ -87,4 +87,103 @@ export const downloadBase64File = (base64: string, filename: string) => {
|
||||
});
|
||||
|
||||
downloadFile(blob, filename);
|
||||
};
|
||||
};
|
||||
|
||||
export type CompressImageFileOptions = {
|
||||
maxWidth?: number;
|
||||
maxHeight?: number;
|
||||
quality?: number;
|
||||
mimeType?: "image/jpeg" | "image/webp" | "image/png";
|
||||
skipBelowBytes?: number;
|
||||
};
|
||||
|
||||
const getCompressedImageName = (fileName: string, mimeType: string) => {
|
||||
const extension = mimeType === "image/png" ? "png" : mimeType === "image/webp" ? "webp" : "jpg";
|
||||
const baseName = fileName.replace(/\.[^.]+$/, "");
|
||||
return `${baseName || "image"}.${extension}`;
|
||||
};
|
||||
|
||||
const loadImageElement = (file: File) => {
|
||||
return new Promise<HTMLImageElement>((resolve, reject) => {
|
||||
const url = URL.createObjectURL(file);
|
||||
const image = new Image();
|
||||
|
||||
image.onload = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
resolve(image);
|
||||
};
|
||||
image.onerror = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
reject(new Error("Failed to load image."));
|
||||
};
|
||||
image.src = url;
|
||||
});
|
||||
};
|
||||
|
||||
const canvasToBlob = (canvas: HTMLCanvasElement, mimeType: string, quality: number) => {
|
||||
return new Promise<Blob>((resolve, reject) => {
|
||||
canvas.toBlob(
|
||||
(blob) => {
|
||||
if (blob) resolve(blob);
|
||||
else reject(new Error("Failed to compress image."));
|
||||
},
|
||||
mimeType,
|
||||
quality
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
export const compressImageFile = async (
|
||||
file: File,
|
||||
options: CompressImageFileOptions = {}
|
||||
) => {
|
||||
if (!file.type.startsWith("image/") || file.type === "image/svg+xml" || file.type === "image/gif") {
|
||||
return file;
|
||||
}
|
||||
|
||||
const {
|
||||
maxWidth = 1600,
|
||||
maxHeight = 1600,
|
||||
quality = 0.75,
|
||||
mimeType = "image/jpeg",
|
||||
skipBelowBytes = 500 * 1024,
|
||||
} = options;
|
||||
|
||||
if (file.size <= skipBelowBytes) return file;
|
||||
|
||||
try {
|
||||
const image = await loadImageElement(file);
|
||||
const ratio = Math.min(maxWidth / image.naturalWidth, maxHeight / image.naturalHeight, 1);
|
||||
const width = Math.max(1, Math.round(image.naturalWidth * ratio));
|
||||
const height = Math.max(1, Math.round(image.naturalHeight * ratio));
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
if (!ctx) return file;
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
if (mimeType === "image/jpeg") {
|
||||
ctx.fillStyle = "#fff";
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
}
|
||||
|
||||
ctx.drawImage(image, 0, 0, width, height);
|
||||
|
||||
const blob = await canvasToBlob(canvas, mimeType, quality);
|
||||
if (blob.size >= file.size) return file;
|
||||
|
||||
return new File([blob], getCompressedImageName(file.name, mimeType), {
|
||||
type: blob.type || mimeType,
|
||||
lastModified: Date.now(),
|
||||
});
|
||||
} catch {
|
||||
return file;
|
||||
}
|
||||
};
|
||||
|
||||
export const compressImageFiles = (
|
||||
files: File[],
|
||||
options?: CompressImageFileOptions
|
||||
) => Promise.all(files.map((file) => compressImageFile(file, options)));
|
||||
|
||||
Reference in New Issue
Block a user