11# Browser Image Compression #
22[ ![ npm] ( https://img.shields.io/npm/v/browser-image-compression.svg )] ( https://www.npmjs.com/package/browser-image-compression )
3+ [ ![ npm] ( ./coverage/badge.svg )] ( https://github.com/Donaldcwl/browser-image-compression )
34[ ![ npm] ( https://img.shields.io/npm/l/browser-image-compression.svg )] ( https://github.com/Donaldcwl/browser-image-compression )
45
56Javascript module to be run in the web browser for image compression.
67
78## Features ##
89- 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.
10+ - ** Multi-thread** (web worker) non-blocking compression are supported through options.
11+
12+ ## Change log ##
13+ v1.0.0
14+ - breaking change: change "imageCompression" function signature
15+ - use of OffscreenCanvas when support, fallback to document.createElement('canvas')
16+ - use createImageBitmap when support, fallback to FileReader readAsDataURL
17+ - add web worker support
18+ - follows image exif orientation
919
1020## Install ##
1121You can download imageCompression from the [ dist folder] [ dist ] . Alternatively, you can install it via yarn or npm
@@ -16,7 +26,7 @@ yarn add browser-image-compression
1626```
1727or use a CDN like [ delivrjs] :
1828```
19- https://cdn.jsdelivr.net/npm/browser-image-compression@0 .0.4 /dist/browser-image-compression.js
29+ https://cdn.jsdelivr.net/npm/browser-image-compression@1 .0.0 /dist/browser-image-compression.js
2030```
2131
2232## How to use this module in your project? ##
@@ -25,63 +35,61 @@ https://cdn.jsdelivr.net/npm/browser-image-compression@0.0.4/dist/browser-image-
2535(can be used in framework like React, Angular, Vue etc)
2636
2737(work with bundler like webpack and rollup)
28- ``` javascriptx
38+ ``` javascript
2939import imageCompression from ' browser-image-compression' ;
3040```
3141
3242or
3343
3444#### In html file ####
3545``` html
36- <script type =" text/javascript" src =" https://cdn.jsdelivr.net/npm/browser-image-compression@0 .0.4 /dist/browser-image-compression.js" ></script >
46+ <script type =" text/javascript" src =" https://cdn.jsdelivr.net/npm/browser-image-compression@1 .0.0 /dist/browser-image-compression.js" ></script >
3747```
3848
3949## API ##
4050### Main function ###
41- #### imageCompression(file: File[ , maxSizeMB: number] [ , maxWidthOrHeight: number ] ): Promise\< File> ####
42- ### Helper function ###
43- #### imageCompression.drawImageInCanvas(img: HTMLImageElement[ , maxWidthOrHeight: number] ): Canvas ####
44- #### imageCompression.getDataUrlFromFile(file: File): Promise\< base64 encoded string> ####
45- #### imageCompression.getFilefromDataUrl(dataUrl: string): Promise\< File> ####
46- #### imageCompression.loadImage(url: string): Promise\< HTMLImageElement> ####
51+ ``` javascript
52+ // you should provide one of maxSizeMB, maxWidthOrHeight in the options
53+ const options = {
54+ maxSizeMB: number, // (default: Number.POSITIVE_INFINITY)
55+ maxWidthOrHeight: number, // compressedFile will scale down by ratio to a point that width or height is smaller than maxWidthOrHeight (default: undefined)
56+ useWebWorker: boolean, // optional, use multi-thread web worker, fallback to run in main-thread (default: true)
57+ maxIteration: number // optional, max number of iteration to compress the image (default: 10)
58+ }
4759
48- ## Usage ##
49- ```
50- <input type="file" accept="image/*" onchange="handleImageUpload(event);">
60+ imageCompression (file: File , options): Promise < File >
5161```
62+ ### Helper function ###
63+ - for advanced user only, most user won't need to use the helper functions
5264``` javascript
53- function handleImageUpload (event ) {
54-
55- var imageFile = event .target .files [0 ];
56- console .log (' originalFile instanceof Blob' , imageFile instanceof Blob ); // true
57- console .log (` originalFile size ${ imageFile .size / 1024 / 1024 } MB` );
58-
59- var maxSizeMB = 1 ;
60- var maxWidthOrHeight = 1920 ; // compressedFile will scale down by ratio to a point that width or height is smaller than maxWidthOrHeight
61- imageCompression (imageFile, maxSizeMB, maxWidthOrHeight) // maxSizeMB, maxWidthOrHeight are optional
62- .then (function (compressedFile ) {
63- console .log (' compressedFile instanceof Blob' , compressedFile instanceof Blob ); // true
64- console .log (` compressedFile size ${ compressedFile .size / 1024 / 1024 } MB` ); // smaller than maxSizeMB
65+ imageCompression .getDataUrlFromFile (file: File ): Promise < base64 encoded string>
66+ imageCompression .getFilefromDataUrl (dataUrl: string): Promise < File >
67+ imageCompression .loadImage (url: string): Promise < HTMLImageElement >
68+ imageCompression .drawImageInCanvas (img: HTMLImageElement ): HTMLCanvasElement
69+ imageCompression .drawFileInCanvas (file: File ): Promise < [ImageBitmap | HTMLImageElement , HTMLCanvasElement ]>
70+ imageCompression .canvasToFile (canvas, fileType, fileName, fileLastModified[, quality]): Promise < File | Blob >
71+ imageCompression .getExifOrientation (file: File ): Promise < number> // based on https://stackoverflow.com/a/32490603/10395024
72+ ```
6573
66- return uploadToServer (compressedFile); // write your own logic
67- })
68- .catch (function (error ) {
69- console .log (error .message );
70- });
71- }
74+ ## Usage ##
75+ ``` html
76+ <input type =" file" accept =" image/*" onchange =" handleImageUpload(event);" >
7277```
73- with async/ await syntax:
78+ async await syntax:
7479``` javascript
7580async function handleImageUpload (event ) {
7681
7782 const imageFile = event .target .files [0 ];
7883 console .log (' originalFile instanceof Blob' , imageFile instanceof Blob ); // true
7984 console .log (` originalFile size ${ imageFile .size / 1024 / 1024 } MB` );
8085
81- const maxSizeMB = 1 ;
82- const maxWidthOrHeight = 1920 ; // compressedFile will scale down by ratio to a point that width or height is smaller than maxWidthOrHeight
86+ var options = {
87+ maxSizeMB: 1 ,
88+ maxWidthOrHeight: 1920 ,
89+ useWebWorker: true
90+ }
8391 try {
84- const compressedFile = await imageCompression (imageFile, maxSizeMB); // maxSizeMB, maxWidthOrHeight are optional
92+ const compressedFile = await imageCompression (imageFile, options);
8593 console .log (' compressedFile instanceof Blob' , compressedFile instanceof Blob ); // true
8694 console .log (` compressedFile size ${ compressedFile .size / 1024 / 1024 } MB` ); // smaller than maxSizeMB
8795
@@ -92,6 +100,31 @@ async function handleImageUpload(event) {
92100
93101}
94102```
103+ Promise.then().catch() syntax:
104+ ``` javascript
105+ function handleImageUpload (event ) {
106+
107+ var imageFile = event .target .files [0 ];
108+ console .log (' originalFile instanceof Blob' , imageFile instanceof Blob ); // true
109+ console .log (` originalFile size ${ imageFile .size / 1024 / 1024 } MB` );
110+
111+ var options = {
112+ maxSizeMB: 1 ,
113+ maxWidthOrHeight: 1920 ,
114+ useWebWorker: true
115+ }
116+ imageCompression (imageFile, options)
117+ .then (function (compressedFile ) {
118+ console .log (' compressedFile instanceof Blob' , compressedFile instanceof Blob ); // true
119+ console .log (` compressedFile size ${ compressedFile .size / 1024 / 1024 } MB` ); // smaller than maxSizeMB
120+
121+ return uploadToServer (compressedFile); // write your own logic
122+ })
123+ .catch (function (error ) {
124+ console .log (error .message );
125+ });
126+ }
127+ ```
95128
96129## Browsers support ##
97130
0 commit comments