-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
image resizer added to generate the imageUrlThumnail, and requested r…
…eviews applied
- Loading branch information
Showing
6 changed files
with
86 additions
and
39 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,50 @@ | ||
const dataUrlToFileConverter = async ( | ||
dataUrl: string, | ||
fileName: string | ||
): Promise<File> => { | ||
const res: Response = await fetch(dataUrl); | ||
const blob: Blob = await res.blob(); | ||
return new File([blob], fileName, { type: "image/png" }); | ||
}; | ||
|
||
const imageResizer = (file: File, callback: (resizedFile: File) => void) => { | ||
if (!file) return; | ||
|
||
const reader = new FileReader(); | ||
|
||
reader.readAsDataURL(file); | ||
|
||
reader.onload = (event) => { | ||
const imageElement: HTMLImageElement = document.createElement("img"); | ||
imageElement.src = `${event.target?.result}`; | ||
|
||
imageElement.onload = async (e) => { | ||
const imageTarget = e.target as HTMLImageElement; | ||
const { width, height } = imageTarget; | ||
|
||
const canvas = document.createElement("canvas"); | ||
const maxWidth = 400; | ||
|
||
const scaleSize = maxWidth / width; | ||
|
||
canvas.width = maxWidth; | ||
canvas.height = height * scaleSize; | ||
|
||
const canvasContext = canvas.getContext("2d"); | ||
canvasContext?.drawImage(imageTarget, 0, 0, canvas.width, canvas.height); | ||
|
||
if (canvasContext) { | ||
const srcEncoded = canvasContext.canvas.toDataURL( | ||
imageTarget.src, | ||
"image/jpeg" | ||
); | ||
|
||
const resizedFile = await dataUrlToFileConverter(srcEncoded, file.name); | ||
|
||
callback(resizedFile); | ||
} | ||
}; | ||
}; | ||
}; | ||
|
||
export default imageResizer; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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