Forked from @Donaldcwl/browser-image-compression
Browser Image Compression
![npm](https://img.shields.io/npm/l/browser-image-compression.svg)
Javascript module to be run in the web browser for image compression.
Features
- You can use this module to compress jpeg and png image by reducing resolution or storage size before uploading to application server to save bandwidth.
- Multi-thread (web worker) non-blocking compression are supported through options.
Install
You can download imageCompression from the dist folder. Alternatively, you can install it via yarn or npm
npm install browser-image-compression --save
or
yarn add browser-image-compression
or use a CDN like delivrjs:
https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.15/dist/browser-image-compression.js
or
https://cdn.jsdelivr.net/npm/browser-image-compression@latest/dist/browser-image-compression.js
Support
If this project help you reduce time to develop, you can buy me a cup of coffee :)
![Buy Me A Coffee](https://cdn.buymeacoffee.com/buttons/v2/default-red.png)
How to use this module in your project?
Use as ES module
(can be used in framework like React, Angular, Vue etc)
(work with bundler like webpack and rollup)
import imageCompression from 'browser-image-compression';
or
In html file
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.15/dist/browser-image-compression.js"></script>
API
Main function
const options = {
maxSizeMB: number,
maxWidthOrHeight: number,
onProgress: Function,
useWebWorker: boolean,
maxIteration: number,
exifOrientation: number,
fileType: string,
initialQuality: number
}
imageCompression(file: File, options): Promise<File>
Caveat
Each browser limits the maximum size of a Canvas object.
So, we resize the image to less than the maximum size that each browser restricts.
(However, the proportion/ratio
of the image remains.)
Helper function
- for advanced users only, most users won't need to use the helper functions
imageCompression.getDataUrlFromFile(file: File): Promise<base64 encoded string>
imageCompression.getFilefromDataUrl(dataUrl: string, filename: string, lastModified?: number): Promise<File>
imageCompression.loadImage(url: string): Promise<HTMLImageElement>
imageCompression.drawImageInCanvas(img: HTMLImageElement): HTMLCanvasElement | OffscreenCanvas
imageCompression.drawFileInCanvas(file: File): Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement | OffscreenCanvas]>
imageCompression.canvasToFile(canvas: HTMLCanvasElement | OffscreenCanvas, fileType: string, fileName: string, fileLastModified: number, quality?: number): Promise<File>
imageCompression.getExifOrientation(file: File): Promise<number>
Usage
<input type="file" accept="image/*" onchange="handleImageUpload(event);">
async await syntax:
async function handleImageUpload(event) {
const imageFile = event.target.files[0];
console.log('originalFile instanceof Blob', imageFile instanceof Blob);
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
const options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true
}
try {
const compressedFile = await imageCompression(imageFile, options);
console.log('compressedFile instanceof Blob', compressedFile instanceof Blob);
console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`);
await uploadToServer(compressedFile);
} catch (error) {
console.log(error);
}
}
Promise.then().catch() syntax:
function handleImageUpload(event) {
var imageFile = event.target.files[0];
console.log('originalFile instanceof Blob', imageFile instanceof Blob);
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
var options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true
}
imageCompression(imageFile, options)
.then(function (compressedFile) {
console.log('compressedFile instanceof Blob', compressedFile instanceof Blob);
console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`);
return uploadToServer(compressedFile);
})
.catch(function (error) {
console.log(error.message);
});
}
Demo / Example
open https://donaldcwl.github.io/browser-image-compression/example/basic.html
or check the "example" folder in this repo
Browsers support
![IE / Edge](https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/edge.png) IE / Edge | ![Firefox](https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/firefox.png) Firefox | ![Chrome](https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome.png) Chrome | ![Safari](https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari.png) Safari | ![Opera](https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/opera.png) Opera | ![Chrome for Android](https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome-android.png) Chrome for Android |
---|
IE10, IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
The browser need to support "OffscreenCanvas" API in order to take advantage of non-blocking compression. If browser do not support "OffscreenCanvas" API, main thread is used instead. See https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas#browser_compatibility for browser compatibility of "OffscreenCanvas" API.
Typescript type definitions
Typescript definitions are included in the package & referenced in the types
section of the package.json
Contribution
- fork the repo and git clone it
- run
npm run watch
# it will watch code change in lib/ folder and generate js in dist/ folder - add/update code in lib/ folder
- try the code by opening example/development.html which will load the js in dist/ folder
- add/update test in test/ folder
npm run test
- push to your forked repo on github
- make a pull request to dev branch of this repo