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 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
  [![Coverage Status](https://img.shields.io/codecov/c/github/fengyuanchen/compressorjs.svg)](https://codecov.io/gh/fengyuanchen/compressorjs) [![Downloads](https://img.shields.io/npm/dm/compressorjs.svg)](https://www.npmjs.com/package/compressorjs) [![Version](https://img.shields.io/npm/v/compressorjs.svg)](https://www.npmjs.com/package/compressorjs) [![Gzip Size](https://img.shields.io/bundlephobia/minzip/compressorjs.svg)](https://unpkg.com/compressorjs/dist/compressor.common.js)
4
4
 
5
- > JavaScript image compressor. Uses the Browser's native [canvas.toBlob](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) API 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 a image on the client side before uploading it.
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 contents
9
+ ## Table of Contents
10
10
 
11
- - [Main](#main)
12
- - [Getting started](#getting-started)
11
+ - [Main Files](#main-files)
12
+ - [Getting Started](#getting-started)
13
13
  - [Options](#options)
14
14
  - [Methods](#methods)
15
- - [No conflict](#no-conflict)
16
- - [Browser support](#browser-support)
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 started
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
- - Type: [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) or [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
51
+ - **options** (optional)
52
+ - Type: `Object`
53
+ - The configuration options. Check out the available [options](#options).
50
54
 
51
- The target image file for compressing.
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 have to access the `result` in the `success` hook function.
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 for server
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
- // Send the compressed image file to server with XMLHttpRequest.
89
- axios.post('/path/to/upload', formData).then(() => {
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, You may use `Compressor.setDefaults(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 [canvas.toBlob](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) for more detail.
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: `['image/png']`
240
+ - Default: `["image/png"]`
232
241
  - Examples:
233
- - `['image/png', 'image/webp']`
234
- - `'image/png,image/webp'`
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 into the canvas for compression.
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 into the canvas for compression.
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 (a `File` (**read only**) or `Blob` object).
309
+ - `result`: The compressed image (`File` in modern browsers, or `Blob` as a fallback).
299
310
 
300
- The hook function to execute when successful to compress the image.
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 fails to compress the image.
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
- ## No conflict
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
- ## Browser support
374
+ [⬆ back to top](#table-of-contents)
375
+
376
+ ## Browser Support
340
377
 
341
378
  - Chrome (latest)
342
379
  - Firefox (latest)