Skip to content

Instantly share code, notes, and snippets.

@ibelick
Created April 7, 2022 09:38
Show Gist options
  • Save ibelick/aeb88474d3c74277bd4ccc3dc03271ec to your computer and use it in GitHub Desktop.
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.
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