Skip to content

Commit

Permalink
fix: canvas final size
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti authored and aykutkardas committed Mar 6, 2024
1 parent 0791990 commit e7330dd
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions lib/image.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export async function resizeImage(
file: File,
max: number = 1024,
): Promise<Blob> {
export async function resizeImage(file: File, max: number = 1024): Promise<Blob> {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

Expand All @@ -12,31 +9,18 @@ export async function resizeImage(
const image = await loadImage(file);

const targetSize = Math.min(max, Math.max(image.width, image.height));
console.log("targetSize", targetSize);

const targetWidth =
image.width > image.height
? targetSize
: Math.round((image.width / image.height) * targetSize);
image.width > image.height ? targetSize : Math.round((image.width / image.height) * targetSize);
const targetHeight =
image.height > image.width
? targetSize
: Math.round((image.height / image.width) * targetSize);
image.height > image.width ? targetSize : Math.round((image.height / image.width) * targetSize);

ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
canvas.width = targetWidth;
canvas.height = targetHeight;

return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => {
if (!blob) {
reject(new Error("Failed to convert canvas to blob"));
return;
}
resolve(blob);
},
"image/jpeg",
0.7,
);
});
ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, targetWidth, targetHeight);

return await canvasToImage(canvas);
}

async function loadImage(file: File): Promise<HTMLImageElement> {
Expand All @@ -52,3 +36,19 @@ async function loadImage(file: File): Promise<HTMLImageElement> {
reader.readAsDataURL(file);
});
}

async function canvasToImage(canvas: HTMLCanvasElement): Promise<Blob> {
return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => {
if (!blob) {
reject(new Error("Failed to convert canvas to blob"));
return;
}
resolve(blob);
},
"image/jpeg",
0.7,
);
});
}

0 comments on commit e7330dd

Please sign in to comment.