-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6904d6b
commit 0791990
Showing
4 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export async function resizeImage( | ||
file: File, | ||
max: number = 1024, | ||
): Promise<Blob> { | ||
const canvas = document.createElement("canvas"); | ||
const ctx = canvas.getContext("2d"); | ||
|
||
if (!ctx) { | ||
throw new Error("Failed to get 2d context"); | ||
} | ||
|
||
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); | ||
const targetHeight = | ||
image.height > image.width | ||
? targetSize | ||
: Math.round((image.height / image.width) * targetSize); | ||
|
||
ctx.drawImage(image, 0, 0, targetWidth, 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, | ||
); | ||
}); | ||
} | ||
|
||
async function loadImage(file: File): Promise<HTMLImageElement> { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = () => { | ||
const image = new Image(); | ||
image.onload = () => resolve(image); | ||
image.onerror = reject; | ||
image.src = reader.result as string; | ||
}; | ||
reader.onerror = reject; | ||
reader.readAsDataURL(file); | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.