You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-5Lines changed: 21 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
Javascript module to be run in the web browser for image compression.
7
7
8
8
## Features
9
-
- You can use this module to compress jpegand png images by reducing **resolution** or **storage size** before uploading to the application server to save bandwidth.
9
+
- You can use this module to compress jpeg, png, webp, and bmp images by reducing **resolution** or **storage size** before uploading to the application server to save bandwidth.
10
10
-**Multi-thread** (web worker) non-blocking compression is supported through options.
11
11
12
12
@@ -34,7 +34,7 @@ async function handleImageUpload(event) {
@@ -107,6 +107,8 @@ If this project helps you reduce the time to develop, you can buy me a cup of co
107
107
108
108
<ahref="https://donaldcwl.github.io/donation/"target="_blank"><imgsrc="https://cdn.buymeacoffee.com/buttons/v2/default-red.png"alt="Buy Me A Coffee"height=60width=217 ></a>
109
109
110
+
(powered by Stripe)
111
+
110
112
## API
111
113
### Main function
112
114
```javascript
@@ -118,8 +120,10 @@ const options: Options = {
118
120
// Please check the Caveat part for details.
119
121
onProgress:Function, // optional, a function takes one progress argument (percentage from 0 to 100)
120
122
useWebWorker: boolean, // optional, use multi-thread web worker, fallback to run in main-thread (default: true)
123
+
libURL: string, // optional, the libURL of this library for importing script in Web Worker (default: https://cdn.jsdelivr.net/npm/browser-image-compression/dist/browser-image-compression.js)
124
+
preserveExif: boolean, // optional, use preserve Exif metadata for JPEG image e.g., Camera model, Focal length, etc (default: false)
121
125
122
-
signal: AbortSignal, //options, to abort / cancel the compression
126
+
signal: AbortSignal, //optional, to abort / cancel the compression
123
127
124
128
// following options are for advanced users
125
129
maxIteration: number, // optional, max number of iteration to compress the image (default: 10)
Each browser limits [the maximum size](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas#maximum_canvas_size) of a Canvas object. <br/>
140
+
Each browser limits [the maximum size](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas#maximum_canvas_size) of a browser Canvas object. <br/>
137
141
So, we resize the image to less than the maximum size that each browser restricts. <br/>
138
142
(However, the `proportion/ratio` of the image remains.)
imageCompression.getExifOrientation(file:File):Promise<number>// based on https://stackoverflow.com/a/32490603/10395024
182
+
imageCompression.copyExifWithoutOrientation(copyExifFromFile:File, copyExifToFile:File):Promise<File>// based on https://gist.github.com/tonytonyjan/ffb7cd0e82cb293b843ece7e79364233
178
183
```
179
184
180
185
@@ -192,6 +197,9 @@ You can include the following script to load the core-js polyfill:
The webp compression is supported on major browsers. Please see https://caniuse.com/mdn-api_offscreencanvas_converttoblob_option_type_parameter_webp for browser compatibility.
202
+
195
203
196
204
## Remarks for compression to work in Web Worker
197
205
The browser needs to support "OffscreenCanvas" API in order to take advantage of non-blocking compression. If the browser does not support "OffscreenCanvas" API, the main thread is used instead. See https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas#browser_compatibility for browser compatibility of "OffscreenCanvas" API.
@@ -201,6 +209,14 @@ The browser needs to support "OffscreenCanvas" API in order to take advantage of
201
209
Typescript definitions are included in the package & referenced in the `types` section of the `package.json`
202
210
203
211
212
+
## Remarks on Content Security Policy (CSP)
213
+
If your website has CSP enabled and you want to use Web Worker (useWebWorker: true), please add the following to the response header
-`https://cdn.jsdelivr.net` is for importing this library from CDN inside Web Worker script. If you don't want to load this library from CDN, you can set your self hosted library URL in `options.libURL`.
218
+
219
+
204
220
## Contribution
205
221
1. fork the repo and git clone it
206
222
2. run `npm run watch` # it will watch code change in lib/ folder and generate js in dist/ folder
0 commit comments