compressorjs 1.2.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +76 -39
- package/dist/compressor.common.js +172 -111
- package/dist/compressor.esm.js +172 -111
- package/dist/compressor.js +172 -111
- package/dist/compressor.min.js +3 -3
- package/package.json +50 -45
- package/src/defaults.js +9 -9
- package/src/index.js +54 -23
- package/src/utilities.js +51 -7
- package/types/index.d.ts +10 -7
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright 2018-present Chen Fengyuan
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright 2018-present Chen Fengyuan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://codecov.io/gh/fengyuanchen/compressorjs) [](https://www.npmjs.com/package/compressorjs) [](https://www.npmjs.com/package/compressorjs) [](https://unpkg.com/compressorjs/dist/compressor.common.js)
|
|
4
4
|
|
|
5
|
-
> JavaScript image compressor. Uses the
|
|
5
|
+
> JavaScript image compressor. Uses the browser's native [HTMLCanvasElement.toBlob()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) method to do the compression work, which means it is **lossy compression**, **asynchronous**, and has **different compression effects in different browsers**. Generally use this to precompress an image on the client side before uploading it.
|
|
6
6
|
|
|
7
7
|
- [Website](https://fengyuanchen.github.io/compressorjs)
|
|
8
8
|
|
|
9
|
-
## Table of
|
|
9
|
+
## Table of Contents
|
|
10
10
|
|
|
11
|
-
- [Main](#main)
|
|
12
|
-
- [Getting
|
|
11
|
+
- [Main Files](#main-files)
|
|
12
|
+
- [Getting Started](#getting-started)
|
|
13
13
|
- [Options](#options)
|
|
14
14
|
- [Methods](#methods)
|
|
15
|
-
- [
|
|
16
|
-
- [Browser
|
|
15
|
+
- [Static Methods](#static-methods)
|
|
16
|
+
- [Browser Support](#browser-support)
|
|
17
17
|
- [Contributing](#contributing)
|
|
18
18
|
- [Versioning](#versioning)
|
|
19
19
|
- [License](#license)
|
|
20
20
|
|
|
21
|
-
## Main
|
|
21
|
+
## Main Files
|
|
22
22
|
|
|
23
23
|
```text
|
|
24
24
|
dist/
|
|
@@ -28,7 +28,7 @@ dist/
|
|
|
28
28
|
└── compressor.esm.js (ES Module)
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
## Getting
|
|
31
|
+
## Getting Started
|
|
32
32
|
|
|
33
33
|
### Install
|
|
34
34
|
|
|
@@ -44,18 +44,15 @@ npm install compressorjs
|
|
|
44
44
|
new Compressor(file[, options])
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
**file**
|
|
47
|
+
- **file**
|
|
48
|
+
- Type: [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) or [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
|
|
49
|
+
- The target image file to compress.
|
|
48
50
|
|
|
49
|
-
-
|
|
51
|
+
- **options** (optional)
|
|
52
|
+
- Type: `Object`
|
|
53
|
+
- The configuration options. Check out the available [options](#options).
|
|
50
54
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
**options**
|
|
54
|
-
|
|
55
|
-
- Type: `Object`
|
|
56
|
-
- Optional
|
|
57
|
-
|
|
58
|
-
The options for compressing. Check out the available [options](#options).
|
|
55
|
+
Alternatively, you may use `Compressor.create(file[, options])`.
|
|
59
56
|
|
|
60
57
|
#### Example
|
|
61
58
|
|
|
@@ -64,7 +61,6 @@ The options for compressing. Check out the available [options](#options).
|
|
|
64
61
|
```
|
|
65
62
|
|
|
66
63
|
```js
|
|
67
|
-
import axios from 'axios';
|
|
68
64
|
import Compressor from 'compressorjs';
|
|
69
65
|
|
|
70
66
|
document.getElementById('file').addEventListener('change', (e) => {
|
|
@@ -78,32 +74,42 @@ document.getElementById('file').addEventListener('change', (e) => {
|
|
|
78
74
|
quality: 0.6,
|
|
79
75
|
|
|
80
76
|
// The compression process is asynchronous,
|
|
81
|
-
// which means you
|
|
77
|
+
// which means you need to access the `result` inside the `success` hook.
|
|
82
78
|
success(result) {
|
|
83
79
|
const formData = new FormData();
|
|
84
80
|
|
|
85
|
-
// The third parameter is required
|
|
86
|
-
formData.append('file', result, result.name);
|
|
81
|
+
// The third parameter is required by the server.
|
|
82
|
+
formData.append('file', result, result.name || 'compressed-image.jpg');
|
|
87
83
|
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
fetch('/path/to/upload', {
|
|
85
|
+
method: 'POST',
|
|
86
|
+
body: formData,
|
|
87
|
+
}).then(() => {
|
|
90
88
|
console.log('Upload success');
|
|
89
|
+
}).catch((error) => {
|
|
90
|
+
console.error('Upload failed', error);
|
|
91
91
|
});
|
|
92
92
|
},
|
|
93
93
|
error(err) {
|
|
94
94
|
console.log(err.message);
|
|
95
95
|
},
|
|
96
96
|
});
|
|
97
|
-
|
|
98
97
|
});
|
|
99
98
|
```
|
|
100
99
|
|
|
101
100
|
[⬆ back to top](#table-of-contents)
|
|
102
101
|
|
|
102
|
+
### Tips
|
|
103
|
+
|
|
104
|
+
- Use `quality` between `0.6` and `0.8` for a good balance between file size and visual quality.
|
|
105
|
+
- Avoid `quality: 1` unless you need the image to stay as close as possible to the original; it can increase the file size.
|
|
106
|
+
- For very large images, set `maxWidth` and `maxHeight` to limit the canvas size and avoid browser memory issues.
|
|
107
|
+
- `convertTypes` and `convertSize` are useful for automatically converting large PNG files to JPEG to reduce file size.
|
|
108
|
+
|
|
103
109
|
## Options
|
|
104
110
|
|
|
105
111
|
You may set compressor options with `new Compressor(file, options)`.
|
|
106
|
-
If you want to change the global default options,
|
|
112
|
+
If you want to change the global default options, you may use `Compressor.setDefaults(options)`.
|
|
107
113
|
|
|
108
114
|
### strict
|
|
109
115
|
|
|
@@ -193,7 +199,7 @@ The height of the output image. If not specified, the natural height of the orig
|
|
|
193
199
|
|
|
194
200
|
Sets how the size of the image should be resized to the container specified by the `width` and `height` options.
|
|
195
201
|
|
|
196
|
-
**Note:** This option only available when both the `width` and `height` options are specified.
|
|
202
|
+
**Note:** This option is only available when both the `width` and `height` options are specified.
|
|
197
203
|
|
|
198
204
|
### quality
|
|
199
205
|
|
|
@@ -202,9 +208,9 @@ Sets how the size of the image should be resized to the container specified by t
|
|
|
202
208
|
|
|
203
209
|
The quality of the output image. It must be a number between `0` and `1`. If this argument is anything else, the default values `0.92` and `0.80` are used for `image/jpeg` and `image/webp` respectively. Other arguments are ignored. Be careful to use `1` as it may make the size of the output image become larger.
|
|
204
210
|
|
|
205
|
-
**Note:** This option only available for `image/jpeg` and `image/webp` images.
|
|
211
|
+
**Note:** This option is only available for `image/jpeg` and `image/webp` images.
|
|
206
212
|
|
|
207
|
-
> Check out [
|
|
213
|
+
> Check out the documentation of the [HTMLCanvasElement.toBlob()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) method for more detail.
|
|
208
214
|
|
|
209
215
|
**Examples**:
|
|
210
216
|
|
|
@@ -222,19 +228,24 @@ The quality of the output image. It must be a number between `0` and `1`. If thi
|
|
|
222
228
|
|
|
223
229
|
- Type: `string`
|
|
224
230
|
- Default: `'auto'`
|
|
231
|
+
- Options: `"auto"`, `"image/png"`, `"image/jpeg"`, and `"image/webp"`.
|
|
225
232
|
|
|
226
233
|
The mime type of the output image. By default, the original mime type of the source image file will be used.
|
|
227
234
|
|
|
235
|
+
> **Note:** Safari does not support `mimeType` conversion to `"image/webp"`. For more details, see the [browser compatibility of the `HTMLCanvasElement.toBlob()` method](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#browser_compatibility).
|
|
236
|
+
|
|
228
237
|
### convertTypes
|
|
229
238
|
|
|
230
239
|
- Type: `Array` or `string` (multiple types should be separated by commas)
|
|
231
|
-
- Default: `[
|
|
240
|
+
- Default: `["image/png"]`
|
|
232
241
|
- Examples:
|
|
233
|
-
- `[
|
|
234
|
-
- `
|
|
242
|
+
- `["image/png", "image/webp"]`
|
|
243
|
+
- `"image/png,image/webp"`
|
|
235
244
|
|
|
236
245
|
Files whose file type is included in this list, and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
237
246
|
|
|
247
|
+
> For image file type support, see the [Image file type and format guide](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types).
|
|
248
|
+
|
|
238
249
|
### convertSize
|
|
239
250
|
|
|
240
251
|
- Type: `number`
|
|
@@ -258,7 +269,7 @@ Files whose file type is included in the `convertTypes` list, and whose file siz
|
|
|
258
269
|
- `context`: The 2d rendering context of the canvas.
|
|
259
270
|
- `canvas`: The canvas for compression.
|
|
260
271
|
|
|
261
|
-
The hook function to execute before drawing the image
|
|
272
|
+
The hook function to execute before drawing the image onto the canvas for compression.
|
|
262
273
|
|
|
263
274
|
```js
|
|
264
275
|
new Compressor(file, {
|
|
@@ -278,7 +289,7 @@ new Compressor(file, {
|
|
|
278
289
|
- `context`: The 2d rendering context of the canvas.
|
|
279
290
|
- `canvas`: The canvas for compression.
|
|
280
291
|
|
|
281
|
-
The hook function to execute after drawing the image
|
|
292
|
+
The hook function to execute after drawing the image onto the canvas for compression.
|
|
282
293
|
|
|
283
294
|
```js
|
|
284
295
|
new Compressor(file, {
|
|
@@ -295,9 +306,9 @@ new Compressor(file, {
|
|
|
295
306
|
- Type: `Function`
|
|
296
307
|
- Default: `null`
|
|
297
308
|
- Parameters:
|
|
298
|
-
- `result`: The compressed image (
|
|
309
|
+
- `result`: The compressed image (`File` in modern browsers, or `Blob` as a fallback).
|
|
299
310
|
|
|
300
|
-
The hook function to execute when
|
|
311
|
+
The hook function to execute when the image has been successfully compressed.
|
|
301
312
|
|
|
302
313
|
### error(err)
|
|
303
314
|
|
|
@@ -306,7 +317,7 @@ The hook function to execute when successful to compress the image.
|
|
|
306
317
|
- Parameters:
|
|
307
318
|
- `err`: The compression error (an `Error` object).
|
|
308
319
|
|
|
309
|
-
The hook function executes when
|
|
320
|
+
The hook function executes when the image compression fails.
|
|
310
321
|
|
|
311
322
|
[⬆ back to top](#table-of-contents)
|
|
312
323
|
|
|
@@ -323,7 +334,31 @@ const compressor = new Compressor(file);
|
|
|
323
334
|
compressor.abort();
|
|
324
335
|
```
|
|
325
336
|
|
|
326
|
-
|
|
337
|
+
[⬆ back to top](#table-of-contents)
|
|
338
|
+
|
|
339
|
+
## Static Methods
|
|
340
|
+
|
|
341
|
+
### create(file[, options])
|
|
342
|
+
|
|
343
|
+
Create a new `Compressor` instance. This is equivalent to `new Compressor(file, options)`.
|
|
344
|
+
|
|
345
|
+
```js
|
|
346
|
+
const compressor = Compressor.create(file, {
|
|
347
|
+
quality: 0.6,
|
|
348
|
+
});
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### setDefaults(options)
|
|
352
|
+
|
|
353
|
+
Change the global default options for subsequently created `Compressor` instances. Instance options override these defaults.
|
|
354
|
+
|
|
355
|
+
```js
|
|
356
|
+
Compressor.setDefaults({
|
|
357
|
+
quality: 0.6,
|
|
358
|
+
});
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### noConflict()
|
|
327
362
|
|
|
328
363
|
If you have to use another compressor with the same namespace, just call the `Compressor.noConflict` static method to revert to it.
|
|
329
364
|
|
|
@@ -336,7 +371,9 @@ If you have to use another compressor with the same namespace, just call the `Co
|
|
|
336
371
|
</script>
|
|
337
372
|
```
|
|
338
373
|
|
|
339
|
-
|
|
374
|
+
[⬆ back to top](#table-of-contents)
|
|
375
|
+
|
|
376
|
+
## Browser Support
|
|
340
377
|
|
|
341
378
|
- Chrome (latest)
|
|
342
379
|
- Firefox (latest)
|