-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize Images for Upload using Canvas
- Loading branch information
1 parent
64a822d
commit ca6ae7c
Showing
7 changed files
with
137 additions
and
134 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
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
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,49 @@ | ||
const MAX_WIDTH = 1280; | ||
const QUALITY = 0.92; | ||
|
||
const readPhoto = async (photo) => { | ||
const canvas = document.createElement("canvas"); | ||
const img = document.createElement("img"); | ||
|
||
img.src = await new Promise((resolve) => { | ||
const reader = new FileReader(); | ||
reader.onload = (e) => resolve(e.target.result); | ||
reader.readAsDataURL(photo); | ||
}); | ||
await new Promise((resolve) => { | ||
img.onload = resolve; | ||
}); | ||
|
||
// draw image in canvas element | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
canvas.getContext("2d").drawImage(img, 0, 0, canvas.width, canvas.height); | ||
|
||
return canvas; | ||
}; | ||
|
||
const scaleCanvas = (canvas, scale) => { | ||
const scaledCanvas = document.createElement("canvas"); | ||
scaledCanvas.width = canvas.width * scale; | ||
scaledCanvas.height = canvas.height * scale; | ||
|
||
scaledCanvas | ||
.getContext("2d") | ||
.drawImage(canvas, 0, 0, scaledCanvas.width, scaledCanvas.height); | ||
|
||
return scaledCanvas; | ||
}; | ||
|
||
export default async (photo) => { | ||
let canvas = await readPhoto(photo); | ||
|
||
while (canvas.width >= 2 * MAX_WIDTH) { | ||
canvas = scaleCanvas(canvas, 0.5); | ||
} | ||
|
||
if (canvas.width > MAX_WIDTH) { | ||
canvas = scaleCanvas(canvas, MAX_WIDTH / canvas.width); | ||
} | ||
|
||
return canvas.toDataURL("image/jpeg", QUALITY); | ||
}; |