feat: updated thumbnail bug fixed

This commit is contained in:
2025-08-23 17:46:52 +05:30
parent e9e1d80b7a
commit decdaaf8c6
2 changed files with 54 additions and 13 deletions

View File

@@ -0,0 +1,39 @@
export async function compressImage(
dataUrl: string,
maxWidth = 400,
maxHeight = 400
): Promise<string> {
return new Promise((resolve) => {
const img = new Image();
img.src = dataUrl;
img.onload = () => {
const { width, height } = img;
// Calculate aspect ratio preserving resize
let newWidth = width;
let newHeight = height;
if (width > height) {
if (width > maxWidth) {
newWidth = maxWidth;
newHeight = (height * maxWidth) / width;
}
} else {
if (height > maxHeight) {
newHeight = maxHeight;
newWidth = (width * maxHeight) / height;
}
}
const offCanvas = document.createElement("canvas");
const ctx = offCanvas.getContext("2d");
offCanvas.width = newWidth;
offCanvas.height = newHeight;
ctx?.drawImage(img, 0, 0, newWidth, newHeight);
resolve(offCanvas.toDataURL("image/png"));
};
});
}