Created
April 7, 2022 09:38
Star
You must be signed in to star a gist
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