Created
April 7, 2022 09:38
-
-
Save ibelick/aeb88474d3c74277bd4ccc3dc03271ec to your computer and use it in GitHub Desktop.
Tiny function in JavaScript/TypeScript to compress image file. Generally use this to precompress a image on the client side before uploading it.
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
import Compressor from "compressorjs"; | |
export const compressImage = async ( | |
file: File, | |
quality: number, | |
maxHeight: number, | |
maxWidth: number, | |
convertSize?: number | |
): Promise<File | Blob> => { | |
return await new Promise((resolve, reject) => { | |
new Compressor(file, { | |
quality, | |
maxHeight, | |
maxWidth, | |
convertSize, | |
success: resolve, | |
error: reject, | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment