Skip to content

Commit a12548a

Browse files
authored
Merge pull request #145 from Donaldcwl/dev
v2.0.0 RC
2 parents 909e24b + d3735bf commit a12548a

18 files changed

+659
-285
lines changed

.babelrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{
22
"presets": [
33
["@babel/preset-env", {
4-
"useBuiltIns": "usage",
5-
"corejs": "3",
64
"targets": {
75
"browsers": ["> 5%", "ie 10-11", "not dead"]
86
}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ dist/
9393
.next/
9494
build/
9595

96-
example-express-server/uploads/
96+
example-express-server/uploads/
97+
98+
stats.html

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## v2.0.0 (13 Apr 2022)
2+
* docs: add CSS animation to example html for showing main thread status
3+
* feature: support AbortController (abort / cancel during compression) [#101](https://github.com/Donaldcwl/browser-image-compression/issues/101)
4+
* feature: options.alwaysKeepResolution (default: false) - keep the resolution (width and height) during compression and reduce the quality only (note that options.maxWidthOrHeight is still applied if set) [#127](https://github.com/Donaldcwl/browser-image-compression/issues/127)
5+
* fixed: Main thread is blocked on Mac device for options.useWebWorker=true [#139](https://github.com/Donaldcwl/browser-image-compression/issues/139)
6+
* updated: dropped core-js to reduce bundle size [#138](https://github.com/Donaldcwl/browser-image-compression/issues/138)
7+
* updated: options.useWebWorker default set to true
8+
19
## v1.0.17 (15 Nov 2021)
210
* feature: apply white background to transparent PNG > JPG conversion if options.fileType is image/jpeg or image/jpg [#119](https://github.com/Donaldcwl/browser-image-compression/issues/119)
311
* fixed: Fixed image cropped on Safari [#118](https://github.com/Donaldcwl/browser-image-compression/issues/118)

README.md

Lines changed: 121 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,29 @@
1-
# Browser Image Compression #
1+
# Browser Image Compression
22
[![npm](https://img.shields.io/npm/v/browser-image-compression.svg)](https://www.npmjs.com/package/browser-image-compression)
33
[![npm](./coverage/badge.svg)](https://github.com/Donaldcwl/browser-image-compression)
44
[![npm](https://img.shields.io/npm/l/browser-image-compression.svg)](https://github.com/Donaldcwl/browser-image-compression)
55

66
Javascript module to be run in the web browser for image compression.
77

8-
## Features ##
9-
- 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-
## Demo / Example ##
12-
open https://donaldcwl.github.io/browser-image-compression/example/basic.html
13-
14-
or check the "[example]" folder in this repo
15-
## Install ##
16-
You can download imageCompression from the [dist folder][dist]. Alternatively, you can install it via yarn or npm
17-
```
18-
npm install browser-image-compression --save
19-
or
20-
yarn add browser-image-compression
21-
```
22-
or use a CDN like [delivrjs]:
23-
```
24-
https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.17/dist/browser-image-compression.js
25-
or
26-
https://cdn.jsdelivr.net/npm/browser-image-compression@latest/dist/browser-image-compression.js
27-
```
8+
## Features
9+
- You can use this module to compress jpeg and png images by reducing **resolution** or **storage size** before uploading to the application server to save bandwidth.
10+
- **Multi-thread** (web worker) non-blocking compression is supported through options.
2811

29-
## How to use this module in your project? ##
30-
#### Use as ES module ####
3112

32-
(can be used in framework like React, Angular, Vue etc)
13+
## Upgrade to version 2
14+
Note that core-js is dropped in version 2, please read the [IE support](#ie-support) section.
3315

34-
(work with bundler like webpack and rollup)
35-
```javascript
36-
import imageCompression from 'browser-image-compression';
37-
```
16+
## Demo / Example
17+
open https://donaldcwl.github.io/browser-image-compression/example/basic.html
3818

39-
or
19+
or check the "[example]" folder in this repo
4020

41-
#### In html file ####
42-
```html
43-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.17/dist/browser-image-compression.js"></script>
44-
```
4521

46-
## Usage ##
22+
## Usage
4723
```html
4824
<input type="file" accept="image/*" onchange="handleImageUpload(event);">
4925
```
50-
async await syntax:
26+
### async await syntax:
5127
```javascript
5228
async function handleImageUpload(event) {
5329

@@ -72,64 +48,124 @@ async function handleImageUpload(event) {
7248

7349
}
7450
```
75-
Promise.then().catch() syntax:
51+
### Promise.then().catch() syntax:
52+
<details>
53+
<summary>Click to expand</summary>
54+
55+
```javascript
56+
function handleImageUpload(event) {
57+
58+
var imageFile = event.target.files[0];
59+
console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true
60+
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
61+
62+
var options = {
63+
maxSizeMB: 1,
64+
maxWidthOrHeight: 1920,
65+
useWebWorker: true
66+
}
67+
imageCompression(imageFile, options)
68+
.then(function (compressedFile) {
69+
console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
70+
console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB
71+
72+
return uploadToServer(compressedFile); // write your own logic
73+
})
74+
.catch(function (error) {
75+
console.log(error.message);
76+
});
77+
}
78+
```
79+
</details>
80+
81+
## Installing
82+
### Use as ES module:
83+
You can install it via npm or yarn
84+
```bash
85+
npm install browser-image-compression --save
86+
# or
87+
yarn add browser-image-compression
88+
```
7689
```javascript
77-
function handleImageUpload(event) {
90+
import imageCompression from 'browser-image-compression';
91+
```
92+
(can be used in frameworks like React, Angular, Vue etc)
7893

79-
var imageFile = event.target.files[0];
80-
console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true
81-
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
94+
(work with bundlers like webpack and rollup)
8295

83-
var options = {
84-
maxSizeMB: 1,
85-
maxWidthOrHeight: 1920,
86-
useWebWorker: true
87-
}
88-
imageCompression(imageFile, options)
89-
.then(function (compressedFile) {
90-
console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
91-
console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB
96+
### (or) Load UMD js file:
97+
You can download imageCompression from the [dist folder][dist].
9298

93-
return uploadToServer(compressedFile); // write your own logic
94-
})
95-
.catch(function (error) {
96-
console.log(error.message);
97-
});
98-
}
99+
Alternatively, you can use a CDN like [delivrjs]:
100+
```html
101+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/browser-image-compression@2.0.0/dist/browser-image-compression.js"></script>
99102
```
100103

104+
101105
## Support
102-
If this project help you reduce time to develop, you can buy me a cup of coffee :)
106+
If this project helps you reduce the time to develop, you can buy me a cup of coffee :)
103107

104108
<a href="https://donaldcwl.github.io/donation/" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-red.png" alt="Buy Me A Coffee" height=60 width=217 ></a>
105-
## API ##
106-
### Main function ###
109+
110+
## API
111+
### Main function
107112
```javascript
108113
// you should provide one of maxSizeMB, maxWidthOrHeight in the options
109114
const options: Options = {
110-
maxSizeMB: number, // (default: Number.POSITIVE_INFINITY)
111-
maxWidthOrHeight: number, // compressedFile will scale down by ratio to a point that width or height is smaller than maxWidthOrHeight (default: undefined)
112-
// but, automatically reduce the size to smaller than the maximum Canvas size supported by each browser.
113-
// Please check the Caveat part for details.
114-
onProgress: Function, // optional, a function takes one progress argument (percentage from 0 to 100)
115-
useWebWorker: boolean, // optional, use multi-thread web worker, fallback to run in main-thread (default: true)
115+
maxSizeMB: number, // (default: Number.POSITIVE_INFINITY)
116+
maxWidthOrHeight: number, // compressedFile will scale down by ratio to a point that width or height is smaller than maxWidthOrHeight (default: undefined)
117+
// but, automatically reduce the size to smaller than the maximum Canvas size supported by each browser.
118+
// Please check the Caveat part for details.
119+
onProgress: Function, // optional, a function takes one progress argument (percentage from 0 to 100)
120+
useWebWorker: boolean, // optional, use multi-thread web worker, fallback to run in main-thread (default: true)
121+
122+
signal: AbortSignal, // options, to abort / cancel the compression
116123

117124
// following options are for advanced users
118-
maxIteration: number, // optional, max number of iteration to compress the image (default: 10)
119-
exifOrientation: number, // optional, see https://stackoverflow.com/a/32490603/10395024
120-
fileType: string, // optional, fileType override
121-
initialQuality: number // optional, initial quality value between 0 and 1 (default: 1)
125+
maxIteration: number, // optional, max number of iteration to compress the image (default: 10)
126+
exifOrientation: number, // optional, see https://stackoverflow.com/a/32490603/10395024
127+
fileType: string, // optional, fileType override e.g., 'image/jpeg', 'image/png' (default: file.type)
128+
initialQuality: number, // optional, initial quality value between 0 and 1 (default: 1)
129+
alwaysKeepResolution: boolean // optional, only reduce quality, always keep width and height (default: false)
122130
}
123131

124132
imageCompression(file: File, options: Options): Promise<File>
125133
```
126134

127-
#### Caveat ####
135+
#### Caveat
128136
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/>
129137
So, we resize the image to less than the maximum size that each browser restricts. <br/>
130138
(However, the `proportion/ratio` of the image remains.)
131139

132-
### Helper function ###
140+
#### Abort / Cancel Compression
141+
To use this feature, please check the browser compatibility: https://caniuse.com/?search=AbortController
142+
```javascript
143+
function handleImageUpload(event) {
144+
145+
var imageFile = event.target.files[0];
146+
147+
var controller = new AbortController();
148+
149+
var options = {
150+
// other options here
151+
signal: controller.signal,
152+
}
153+
imageCompression(imageFile, options)
154+
.then(function (compressedFile) {
155+
return uploadToServer(compressedFile); // write your own logic
156+
})
157+
.catch(function (error) {
158+
console.log(error.message); // output: I just want to stop
159+
});
160+
161+
// simulate abort the compression after 1.5 seconds
162+
setTimeout(function () {
163+
controller.abort(new Error('I just want to stop'));
164+
}, 1500);
165+
}
166+
```
167+
168+
### Helper function
133169
- for advanced users only, most users won't need to use the helper functions
134170
```javascript
135171
imageCompression.getDataUrlFromFile(file: File): Promise<base64 encoded string>
@@ -141,19 +177,31 @@ imageCompression.canvasToFile(canvas: HTMLCanvasElement | OffscreenCanvas, fileT
141177
imageCompression.getExifOrientation(file: File): Promise<number> // based on https://stackoverflow.com/a/32490603/10395024
142178
```
143179

180+
144181
## Browsers support
145182

146183
| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br/>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br/>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br/>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br/>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari-ios/safari-ios_48x48.png" alt="iOS Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br/>iOS Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png" alt="Opera" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br/>Opera |
147184
| --------- | --------- | --------- | --------- | --------- | --------- |
148185
| IE10, IE11, Edge| last 2 versions| last 2 versions| last 2 versions| last 2 versions| last 2 versions
149186

187+
### IE support
188+
This library uses ES features such as Promise API, globalThis. If you need to support browsers that do not support new ES features like IE. You can include the core-js polyfill in your project.
189+
190+
You can include the following script to load the core-js polyfill:
191+
```html
192+
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/3.21.1/minified.min.js"></script>
193+
```
194+
195+
150196
## Remarks for compression to work in Web Worker
151-
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.
197+
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.
152198

153-
## Typescript type definitions ##
199+
200+
## Typescript type definitions
154201
Typescript definitions are included in the package & referenced in the `types` section of the `package.json`
155202

156-
## Contribution ##
203+
204+
## Contribution
157205
1. fork the repo and git clone it
158206
2. run `npm run watch` # it will watch code change in lib/ folder and generate js in dist/ folder
159207
3. add/update code in lib/ folder

example-express-server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ yarn dev
1515
## Sample frontend HTML
1616
```html
1717
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
18-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.17/dist/browser-image-compression.js"></script>
18+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/browser-image-compression@2.0.0/dist/browser-image-compression.js"></script>
1919
<input type="file" accept="image/*" onchange="compressImage(event);">
2020
<script>
2121
function compressImage (event) {

0 commit comments

Comments
 (0)