compressorjs 1.3.0 → 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/README.md +65 -33
- package/dist/compressor.common.js +47 -28
- package/dist/compressor.esm.js +47 -28
- package/dist/compressor.js +47 -28
- package/dist/compressor.min.js +3 -3
- package/package.json +12 -12
- package/src/defaults.js +9 -9
- package/src/index.js +19 -9
- package/src/utilities.js +5 -5
- package/types/index.d.ts +10 -7
package/README.md
CHANGED
|
@@ -2,18 +2,18 @@
|
|
|
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
11
|
- [Main Files](#main-files)
|
|
12
|
-
- [Getting
|
|
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)
|
|
@@ -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,7 +208,7 @@ 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
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
|
|
|
@@ -263,7 +269,7 @@ Files whose file type is included in the `convertTypes` list, and whose file siz
|
|
|
263
269
|
- `context`: The 2d rendering context of the canvas.
|
|
264
270
|
- `canvas`: The canvas for compression.
|
|
265
271
|
|
|
266
|
-
The hook function to execute before drawing the image
|
|
272
|
+
The hook function to execute before drawing the image onto the canvas for compression.
|
|
267
273
|
|
|
268
274
|
```js
|
|
269
275
|
new Compressor(file, {
|
|
@@ -283,7 +289,7 @@ new Compressor(file, {
|
|
|
283
289
|
- `context`: The 2d rendering context of the canvas.
|
|
284
290
|
- `canvas`: The canvas for compression.
|
|
285
291
|
|
|
286
|
-
The hook function to execute after drawing the image
|
|
292
|
+
The hook function to execute after drawing the image onto the canvas for compression.
|
|
287
293
|
|
|
288
294
|
```js
|
|
289
295
|
new Compressor(file, {
|
|
@@ -300,9 +306,9 @@ new Compressor(file, {
|
|
|
300
306
|
- Type: `Function`
|
|
301
307
|
- Default: `null`
|
|
302
308
|
- Parameters:
|
|
303
|
-
- `result`: The compressed image (
|
|
309
|
+
- `result`: The compressed image (`File` in modern browsers, or `Blob` as a fallback).
|
|
304
310
|
|
|
305
|
-
The hook function to execute when
|
|
311
|
+
The hook function to execute when the image has been successfully compressed.
|
|
306
312
|
|
|
307
313
|
### error(err)
|
|
308
314
|
|
|
@@ -311,7 +317,7 @@ The hook function to execute when successful to compress the image.
|
|
|
311
317
|
- Parameters:
|
|
312
318
|
- `err`: The compression error (an `Error` object).
|
|
313
319
|
|
|
314
|
-
The hook function executes when
|
|
320
|
+
The hook function executes when the image compression fails.
|
|
315
321
|
|
|
316
322
|
[⬆ back to top](#table-of-contents)
|
|
317
323
|
|
|
@@ -328,7 +334,31 @@ const compressor = new Compressor(file);
|
|
|
328
334
|
compressor.abort();
|
|
329
335
|
```
|
|
330
336
|
|
|
331
|
-
|
|
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()
|
|
332
362
|
|
|
333
363
|
If you have to use another compressor with the same namespace, just call the `Compressor.noConflict` static method to revert to it.
|
|
334
364
|
|
|
@@ -341,7 +371,9 @@ If you have to use another compressor with the same namespace, just call the `Co
|
|
|
341
371
|
</script>
|
|
342
372
|
```
|
|
343
373
|
|
|
344
|
-
|
|
374
|
+
[⬆ back to top](#table-of-contents)
|
|
375
|
+
|
|
376
|
+
## Browser Support
|
|
345
377
|
|
|
346
378
|
- Chrome (latest)
|
|
347
379
|
- Firefox (latest)
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Compressor.js v1.
|
|
2
|
+
* Compressor.js v1.4.0
|
|
3
3
|
* https://fengyuanchen.github.io/compressorjs
|
|
4
4
|
*
|
|
5
5
|
* Copyright 2018-present Chen Fengyuan
|
|
6
6
|
* Released under the MIT license
|
|
7
7
|
*
|
|
8
|
-
* Date: 2026-
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
@@ -77,6 +77,10 @@ function _toPropertyKey(t) {
|
|
|
77
77
|
return "symbol" == typeof i ? i : i + "";
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
function getDefaultExportFromCjs (x) {
|
|
81
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
82
|
+
}
|
|
83
|
+
|
|
80
84
|
var canvasToBlob = {exports: {}};
|
|
81
85
|
|
|
82
86
|
/*
|
|
@@ -190,7 +194,8 @@ var canvasToBlob = {exports: {}};
|
|
|
190
194
|
}
|
|
191
195
|
})(window);
|
|
192
196
|
})(canvasToBlob);
|
|
193
|
-
var
|
|
197
|
+
var canvasToBlobExports = canvasToBlob.exports;
|
|
198
|
+
var toBlob = /*@__PURE__*/getDefaultExportFromCjs(canvasToBlobExports);
|
|
194
199
|
|
|
195
200
|
var isBlob = function isBlob(value) {
|
|
196
201
|
if (typeof Blob === 'undefined') {
|
|
@@ -198,22 +203,23 @@ var isBlob = function isBlob(value) {
|
|
|
198
203
|
}
|
|
199
204
|
return value instanceof Blob || Object.prototype.toString.call(value) === '[object Blob]';
|
|
200
205
|
};
|
|
206
|
+
var isBlob$1 = /*@__PURE__*/getDefaultExportFromCjs(isBlob);
|
|
201
207
|
|
|
202
208
|
var DEFAULTS = {
|
|
203
209
|
/**
|
|
204
|
-
* Indicates
|
|
210
|
+
* Indicates whether to output the original image instead of the compressed one
|
|
205
211
|
* when the size of the compressed image is greater than the original one's
|
|
206
212
|
* @type {boolean}
|
|
207
213
|
*/
|
|
208
214
|
strict: true,
|
|
209
215
|
/**
|
|
210
|
-
* Indicates
|
|
216
|
+
* Indicates whether to read the image's Exif Orientation information,
|
|
211
217
|
* and then rotate or flip the image automatically.
|
|
212
218
|
* @type {boolean}
|
|
213
219
|
*/
|
|
214
220
|
checkOrientation: true,
|
|
215
221
|
/**
|
|
216
|
-
* Indicates
|
|
222
|
+
* Indicates whether to retain the image's Exif information after compression.
|
|
217
223
|
* @type {boolean}
|
|
218
224
|
*/
|
|
219
225
|
retainExif: false,
|
|
@@ -272,7 +278,7 @@ var DEFAULTS = {
|
|
|
272
278
|
/**
|
|
273
279
|
* Files whose file type is included in this list,
|
|
274
280
|
* and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
275
|
-
* @type {string
|
|
281
|
+
* @type {string|string[]}
|
|
276
282
|
*/
|
|
277
283
|
convertTypes: ['image/png'],
|
|
278
284
|
/**
|
|
@@ -282,9 +288,9 @@ var DEFAULTS = {
|
|
|
282
288
|
*/
|
|
283
289
|
convertSize: 5000000,
|
|
284
290
|
/**
|
|
285
|
-
* The hook function to execute before
|
|
291
|
+
* The hook function to execute before drawing the image into the canvas for compression.
|
|
286
292
|
* @type {Function}
|
|
287
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
293
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
288
294
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
289
295
|
* @example
|
|
290
296
|
* function (context, canvas) {
|
|
@@ -293,9 +299,9 @@ var DEFAULTS = {
|
|
|
293
299
|
*/
|
|
294
300
|
beforeDraw: null,
|
|
295
301
|
/**
|
|
296
|
-
* The hook function to execute after
|
|
302
|
+
* The hook function to execute after drawing the image into the canvas for compression.
|
|
297
303
|
* @type {Function}
|
|
298
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
304
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
299
305
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
300
306
|
* @example
|
|
301
307
|
* function (context, canvas) {
|
|
@@ -304,7 +310,7 @@ var DEFAULTS = {
|
|
|
304
310
|
*/
|
|
305
311
|
drew: null,
|
|
306
312
|
/**
|
|
307
|
-
* The hook function to execute when
|
|
313
|
+
* The hook function to execute when the image is compressed successfully.
|
|
308
314
|
* @type {Function}
|
|
309
315
|
* @param {File} file - The compressed image File object.
|
|
310
316
|
* @example
|
|
@@ -349,9 +355,9 @@ function toArray(value) {
|
|
|
349
355
|
var REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
350
356
|
|
|
351
357
|
/**
|
|
352
|
-
* Check if the given value is
|
|
358
|
+
* Check if the given value is an image MIME type.
|
|
353
359
|
* @param {*} value - The value to check.
|
|
354
|
-
* @returns {boolean} Returns `true` if the given is
|
|
360
|
+
* @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
|
|
355
361
|
*/
|
|
356
362
|
function isImageType(value) {
|
|
357
363
|
return REGEXP_IMAGE_TYPE.test(value);
|
|
@@ -360,7 +366,7 @@ function isImageType(value) {
|
|
|
360
366
|
/**
|
|
361
367
|
* Convert image type to extension.
|
|
362
368
|
* @param {string} value - The image type to convert.
|
|
363
|
-
* @returns {
|
|
369
|
+
* @returns {string} Returns the image extension.
|
|
364
370
|
*/
|
|
365
371
|
function imageTypeToExtension(value) {
|
|
366
372
|
var extension = isImageType(value) ? value.substr(6) : '';
|
|
@@ -390,7 +396,7 @@ function getStringFromCharCode(dataView, start, length) {
|
|
|
390
396
|
|
|
391
397
|
/**
|
|
392
398
|
* Check if `canvas.getContext('2d').getImageData` is available,
|
|
393
|
-
*
|
|
399
|
+
* Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
|
|
394
400
|
* @link https://github.com/nodeca/pica/blob/master/lib/utils.js
|
|
395
401
|
* @returns {boolean} Returns `true` if it is available, else `false`.
|
|
396
402
|
*/
|
|
@@ -435,7 +441,7 @@ var btoa = WINDOW.btoa;
|
|
|
435
441
|
/**
|
|
436
442
|
* Transform array buffer to Data URL.
|
|
437
443
|
* @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
|
|
438
|
-
* @param {string} mimeType - The
|
|
444
|
+
* @param {string} mimeType - The MIME type of the Data URL.
|
|
439
445
|
* @returns {string} The result Data URL.
|
|
440
446
|
*/
|
|
441
447
|
function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
@@ -683,11 +689,12 @@ var AnotherCompressor = WINDOW.Compressor;
|
|
|
683
689
|
*/
|
|
684
690
|
var Compressor = /*#__PURE__*/function () {
|
|
685
691
|
/**
|
|
686
|
-
*
|
|
687
|
-
* @param {File|Blob} file - The target image file
|
|
688
|
-
* @param {Object} [options] - The options
|
|
692
|
+
* Create a new Compressor.
|
|
693
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
694
|
+
* @param {Object} [options={}] - The configuration options.
|
|
689
695
|
*/
|
|
690
|
-
function Compressor(file
|
|
696
|
+
function Compressor(file) {
|
|
697
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
691
698
|
_classCallCheck(this, Compressor);
|
|
692
699
|
this.file = file;
|
|
693
700
|
this.exif = [];
|
|
@@ -703,7 +710,7 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
703
710
|
var _this = this;
|
|
704
711
|
var file = this.file,
|
|
705
712
|
options = this.options;
|
|
706
|
-
if (!isBlob(file)) {
|
|
713
|
+
if (!isBlob$1(file)) {
|
|
707
714
|
this.fail(new Error('The first argument must be a File or Blob object.'));
|
|
708
715
|
return;
|
|
709
716
|
}
|
|
@@ -1074,14 +1081,15 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
1074
1081
|
}
|
|
1075
1082
|
|
|
1076
1083
|
/**
|
|
1077
|
-
*
|
|
1078
|
-
* @
|
|
1084
|
+
* Creates a new Compressor instance.
|
|
1085
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
1086
|
+
* @param {Object} [options={}] - The configuration options.
|
|
1087
|
+
* @returns {Compressor} A new Compressor instance.
|
|
1079
1088
|
*/
|
|
1080
1089
|
}], [{
|
|
1081
|
-
key: "
|
|
1082
|
-
value: function
|
|
1083
|
-
|
|
1084
|
-
return Compressor;
|
|
1090
|
+
key: "create",
|
|
1091
|
+
value: function create(file, options) {
|
|
1092
|
+
return new Compressor(file, options);
|
|
1085
1093
|
}
|
|
1086
1094
|
|
|
1087
1095
|
/**
|
|
@@ -1093,6 +1101,17 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
1093
1101
|
value: function setDefaults(options) {
|
|
1094
1102
|
_extends(DEFAULTS, options);
|
|
1095
1103
|
}
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* Get the no conflict compressor class.
|
|
1107
|
+
* @returns {Compressor} The compressor class.
|
|
1108
|
+
*/
|
|
1109
|
+
}, {
|
|
1110
|
+
key: "noConflict",
|
|
1111
|
+
value: function noConflict() {
|
|
1112
|
+
window.Compressor = AnotherCompressor;
|
|
1113
|
+
return Compressor;
|
|
1114
|
+
}
|
|
1096
1115
|
}]);
|
|
1097
1116
|
}();
|
|
1098
1117
|
|
package/dist/compressor.esm.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Compressor.js v1.
|
|
2
|
+
* Compressor.js v1.4.0
|
|
3
3
|
* https://fengyuanchen.github.io/compressorjs
|
|
4
4
|
*
|
|
5
5
|
* Copyright 2018-present Chen Fengyuan
|
|
6
6
|
* Released under the MIT license
|
|
7
7
|
*
|
|
8
|
-
* Date: 2026-
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
function _classCallCheck(a, n) {
|
|
@@ -75,6 +75,10 @@ function _toPropertyKey(t) {
|
|
|
75
75
|
return "symbol" == typeof i ? i : i + "";
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
function getDefaultExportFromCjs (x) {
|
|
79
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
80
|
+
}
|
|
81
|
+
|
|
78
82
|
var canvasToBlob = {exports: {}};
|
|
79
83
|
|
|
80
84
|
/*
|
|
@@ -188,7 +192,8 @@ var canvasToBlob = {exports: {}};
|
|
|
188
192
|
}
|
|
189
193
|
})(window);
|
|
190
194
|
})(canvasToBlob);
|
|
191
|
-
var
|
|
195
|
+
var canvasToBlobExports = canvasToBlob.exports;
|
|
196
|
+
var toBlob = /*@__PURE__*/getDefaultExportFromCjs(canvasToBlobExports);
|
|
192
197
|
|
|
193
198
|
var isBlob = function isBlob(value) {
|
|
194
199
|
if (typeof Blob === 'undefined') {
|
|
@@ -196,22 +201,23 @@ var isBlob = function isBlob(value) {
|
|
|
196
201
|
}
|
|
197
202
|
return value instanceof Blob || Object.prototype.toString.call(value) === '[object Blob]';
|
|
198
203
|
};
|
|
204
|
+
var isBlob$1 = /*@__PURE__*/getDefaultExportFromCjs(isBlob);
|
|
199
205
|
|
|
200
206
|
var DEFAULTS = {
|
|
201
207
|
/**
|
|
202
|
-
* Indicates
|
|
208
|
+
* Indicates whether to output the original image instead of the compressed one
|
|
203
209
|
* when the size of the compressed image is greater than the original one's
|
|
204
210
|
* @type {boolean}
|
|
205
211
|
*/
|
|
206
212
|
strict: true,
|
|
207
213
|
/**
|
|
208
|
-
* Indicates
|
|
214
|
+
* Indicates whether to read the image's Exif Orientation information,
|
|
209
215
|
* and then rotate or flip the image automatically.
|
|
210
216
|
* @type {boolean}
|
|
211
217
|
*/
|
|
212
218
|
checkOrientation: true,
|
|
213
219
|
/**
|
|
214
|
-
* Indicates
|
|
220
|
+
* Indicates whether to retain the image's Exif information after compression.
|
|
215
221
|
* @type {boolean}
|
|
216
222
|
*/
|
|
217
223
|
retainExif: false,
|
|
@@ -270,7 +276,7 @@ var DEFAULTS = {
|
|
|
270
276
|
/**
|
|
271
277
|
* Files whose file type is included in this list,
|
|
272
278
|
* and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
273
|
-
* @type {string
|
|
279
|
+
* @type {string|string[]}
|
|
274
280
|
*/
|
|
275
281
|
convertTypes: ['image/png'],
|
|
276
282
|
/**
|
|
@@ -280,9 +286,9 @@ var DEFAULTS = {
|
|
|
280
286
|
*/
|
|
281
287
|
convertSize: 5000000,
|
|
282
288
|
/**
|
|
283
|
-
* The hook function to execute before
|
|
289
|
+
* The hook function to execute before drawing the image into the canvas for compression.
|
|
284
290
|
* @type {Function}
|
|
285
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
291
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
286
292
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
287
293
|
* @example
|
|
288
294
|
* function (context, canvas) {
|
|
@@ -291,9 +297,9 @@ var DEFAULTS = {
|
|
|
291
297
|
*/
|
|
292
298
|
beforeDraw: null,
|
|
293
299
|
/**
|
|
294
|
-
* The hook function to execute after
|
|
300
|
+
* The hook function to execute after drawing the image into the canvas for compression.
|
|
295
301
|
* @type {Function}
|
|
296
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
302
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
297
303
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
298
304
|
* @example
|
|
299
305
|
* function (context, canvas) {
|
|
@@ -302,7 +308,7 @@ var DEFAULTS = {
|
|
|
302
308
|
*/
|
|
303
309
|
drew: null,
|
|
304
310
|
/**
|
|
305
|
-
* The hook function to execute when
|
|
311
|
+
* The hook function to execute when the image is compressed successfully.
|
|
306
312
|
* @type {Function}
|
|
307
313
|
* @param {File} file - The compressed image File object.
|
|
308
314
|
* @example
|
|
@@ -347,9 +353,9 @@ function toArray(value) {
|
|
|
347
353
|
var REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
348
354
|
|
|
349
355
|
/**
|
|
350
|
-
* Check if the given value is
|
|
356
|
+
* Check if the given value is an image MIME type.
|
|
351
357
|
* @param {*} value - The value to check.
|
|
352
|
-
* @returns {boolean} Returns `true` if the given is
|
|
358
|
+
* @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
|
|
353
359
|
*/
|
|
354
360
|
function isImageType(value) {
|
|
355
361
|
return REGEXP_IMAGE_TYPE.test(value);
|
|
@@ -358,7 +364,7 @@ function isImageType(value) {
|
|
|
358
364
|
/**
|
|
359
365
|
* Convert image type to extension.
|
|
360
366
|
* @param {string} value - The image type to convert.
|
|
361
|
-
* @returns {
|
|
367
|
+
* @returns {string} Returns the image extension.
|
|
362
368
|
*/
|
|
363
369
|
function imageTypeToExtension(value) {
|
|
364
370
|
var extension = isImageType(value) ? value.substr(6) : '';
|
|
@@ -388,7 +394,7 @@ function getStringFromCharCode(dataView, start, length) {
|
|
|
388
394
|
|
|
389
395
|
/**
|
|
390
396
|
* Check if `canvas.getContext('2d').getImageData` is available,
|
|
391
|
-
*
|
|
397
|
+
* Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
|
|
392
398
|
* @link https://github.com/nodeca/pica/blob/master/lib/utils.js
|
|
393
399
|
* @returns {boolean} Returns `true` if it is available, else `false`.
|
|
394
400
|
*/
|
|
@@ -433,7 +439,7 @@ var btoa = WINDOW.btoa;
|
|
|
433
439
|
/**
|
|
434
440
|
* Transform array buffer to Data URL.
|
|
435
441
|
* @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
|
|
436
|
-
* @param {string} mimeType - The
|
|
442
|
+
* @param {string} mimeType - The MIME type of the Data URL.
|
|
437
443
|
* @returns {string} The result Data URL.
|
|
438
444
|
*/
|
|
439
445
|
function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
@@ -681,11 +687,12 @@ var AnotherCompressor = WINDOW.Compressor;
|
|
|
681
687
|
*/
|
|
682
688
|
var Compressor = /*#__PURE__*/function () {
|
|
683
689
|
/**
|
|
684
|
-
*
|
|
685
|
-
* @param {File|Blob} file - The target image file
|
|
686
|
-
* @param {Object} [options] - The options
|
|
690
|
+
* Create a new Compressor.
|
|
691
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
692
|
+
* @param {Object} [options={}] - The configuration options.
|
|
687
693
|
*/
|
|
688
|
-
function Compressor(file
|
|
694
|
+
function Compressor(file) {
|
|
695
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
689
696
|
_classCallCheck(this, Compressor);
|
|
690
697
|
this.file = file;
|
|
691
698
|
this.exif = [];
|
|
@@ -701,7 +708,7 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
701
708
|
var _this = this;
|
|
702
709
|
var file = this.file,
|
|
703
710
|
options = this.options;
|
|
704
|
-
if (!isBlob(file)) {
|
|
711
|
+
if (!isBlob$1(file)) {
|
|
705
712
|
this.fail(new Error('The first argument must be a File or Blob object.'));
|
|
706
713
|
return;
|
|
707
714
|
}
|
|
@@ -1072,14 +1079,15 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
1072
1079
|
}
|
|
1073
1080
|
|
|
1074
1081
|
/**
|
|
1075
|
-
*
|
|
1076
|
-
* @
|
|
1082
|
+
* Creates a new Compressor instance.
|
|
1083
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
1084
|
+
* @param {Object} [options={}] - The configuration options.
|
|
1085
|
+
* @returns {Compressor} A new Compressor instance.
|
|
1077
1086
|
*/
|
|
1078
1087
|
}], [{
|
|
1079
|
-
key: "
|
|
1080
|
-
value: function
|
|
1081
|
-
|
|
1082
|
-
return Compressor;
|
|
1088
|
+
key: "create",
|
|
1089
|
+
value: function create(file, options) {
|
|
1090
|
+
return new Compressor(file, options);
|
|
1083
1091
|
}
|
|
1084
1092
|
|
|
1085
1093
|
/**
|
|
@@ -1091,6 +1099,17 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
1091
1099
|
value: function setDefaults(options) {
|
|
1092
1100
|
_extends(DEFAULTS, options);
|
|
1093
1101
|
}
|
|
1102
|
+
|
|
1103
|
+
/**
|
|
1104
|
+
* Get the no conflict compressor class.
|
|
1105
|
+
* @returns {Compressor} The compressor class.
|
|
1106
|
+
*/
|
|
1107
|
+
}, {
|
|
1108
|
+
key: "noConflict",
|
|
1109
|
+
value: function noConflict() {
|
|
1110
|
+
window.Compressor = AnotherCompressor;
|
|
1111
|
+
return Compressor;
|
|
1112
|
+
}
|
|
1094
1113
|
}]);
|
|
1095
1114
|
}();
|
|
1096
1115
|
|
package/dist/compressor.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Compressor.js v1.
|
|
2
|
+
* Compressor.js v1.4.0
|
|
3
3
|
* https://fengyuanchen.github.io/compressorjs
|
|
4
4
|
*
|
|
5
5
|
* Copyright 2018-present Chen Fengyuan
|
|
6
6
|
* Released under the MIT license
|
|
7
7
|
*
|
|
8
|
-
* Date: 2026-
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
(function (global, factory) {
|
|
@@ -81,6 +81,10 @@
|
|
|
81
81
|
return "symbol" == typeof i ? i : i + "";
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
function getDefaultExportFromCjs (x) {
|
|
85
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
86
|
+
}
|
|
87
|
+
|
|
84
88
|
var canvasToBlob = {exports: {}};
|
|
85
89
|
|
|
86
90
|
/*
|
|
@@ -194,7 +198,8 @@
|
|
|
194
198
|
}
|
|
195
199
|
})(window);
|
|
196
200
|
})(canvasToBlob);
|
|
197
|
-
var
|
|
201
|
+
var canvasToBlobExports = canvasToBlob.exports;
|
|
202
|
+
var toBlob = /*@__PURE__*/getDefaultExportFromCjs(canvasToBlobExports);
|
|
198
203
|
|
|
199
204
|
var isBlob = function isBlob(value) {
|
|
200
205
|
if (typeof Blob === 'undefined') {
|
|
@@ -202,22 +207,23 @@
|
|
|
202
207
|
}
|
|
203
208
|
return value instanceof Blob || Object.prototype.toString.call(value) === '[object Blob]';
|
|
204
209
|
};
|
|
210
|
+
var isBlob$1 = /*@__PURE__*/getDefaultExportFromCjs(isBlob);
|
|
205
211
|
|
|
206
212
|
var DEFAULTS = {
|
|
207
213
|
/**
|
|
208
|
-
* Indicates
|
|
214
|
+
* Indicates whether to output the original image instead of the compressed one
|
|
209
215
|
* when the size of the compressed image is greater than the original one's
|
|
210
216
|
* @type {boolean}
|
|
211
217
|
*/
|
|
212
218
|
strict: true,
|
|
213
219
|
/**
|
|
214
|
-
* Indicates
|
|
220
|
+
* Indicates whether to read the image's Exif Orientation information,
|
|
215
221
|
* and then rotate or flip the image automatically.
|
|
216
222
|
* @type {boolean}
|
|
217
223
|
*/
|
|
218
224
|
checkOrientation: true,
|
|
219
225
|
/**
|
|
220
|
-
* Indicates
|
|
226
|
+
* Indicates whether to retain the image's Exif information after compression.
|
|
221
227
|
* @type {boolean}
|
|
222
228
|
*/
|
|
223
229
|
retainExif: false,
|
|
@@ -276,7 +282,7 @@
|
|
|
276
282
|
/**
|
|
277
283
|
* Files whose file type is included in this list,
|
|
278
284
|
* and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
279
|
-
* @type {string
|
|
285
|
+
* @type {string|string[]}
|
|
280
286
|
*/
|
|
281
287
|
convertTypes: ['image/png'],
|
|
282
288
|
/**
|
|
@@ -286,9 +292,9 @@
|
|
|
286
292
|
*/
|
|
287
293
|
convertSize: 5000000,
|
|
288
294
|
/**
|
|
289
|
-
* The hook function to execute before
|
|
295
|
+
* The hook function to execute before drawing the image into the canvas for compression.
|
|
290
296
|
* @type {Function}
|
|
291
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
297
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
292
298
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
293
299
|
* @example
|
|
294
300
|
* function (context, canvas) {
|
|
@@ -297,9 +303,9 @@
|
|
|
297
303
|
*/
|
|
298
304
|
beforeDraw: null,
|
|
299
305
|
/**
|
|
300
|
-
* The hook function to execute after
|
|
306
|
+
* The hook function to execute after drawing the image into the canvas for compression.
|
|
301
307
|
* @type {Function}
|
|
302
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
308
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
303
309
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
304
310
|
* @example
|
|
305
311
|
* function (context, canvas) {
|
|
@@ -308,7 +314,7 @@
|
|
|
308
314
|
*/
|
|
309
315
|
drew: null,
|
|
310
316
|
/**
|
|
311
|
-
* The hook function to execute when
|
|
317
|
+
* The hook function to execute when the image is compressed successfully.
|
|
312
318
|
* @type {Function}
|
|
313
319
|
* @param {File} file - The compressed image File object.
|
|
314
320
|
* @example
|
|
@@ -353,9 +359,9 @@
|
|
|
353
359
|
var REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
354
360
|
|
|
355
361
|
/**
|
|
356
|
-
* Check if the given value is
|
|
362
|
+
* Check if the given value is an image MIME type.
|
|
357
363
|
* @param {*} value - The value to check.
|
|
358
|
-
* @returns {boolean} Returns `true` if the given is
|
|
364
|
+
* @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
|
|
359
365
|
*/
|
|
360
366
|
function isImageType(value) {
|
|
361
367
|
return REGEXP_IMAGE_TYPE.test(value);
|
|
@@ -364,7 +370,7 @@
|
|
|
364
370
|
/**
|
|
365
371
|
* Convert image type to extension.
|
|
366
372
|
* @param {string} value - The image type to convert.
|
|
367
|
-
* @returns {
|
|
373
|
+
* @returns {string} Returns the image extension.
|
|
368
374
|
*/
|
|
369
375
|
function imageTypeToExtension(value) {
|
|
370
376
|
var extension = isImageType(value) ? value.substr(6) : '';
|
|
@@ -394,7 +400,7 @@
|
|
|
394
400
|
|
|
395
401
|
/**
|
|
396
402
|
* Check if `canvas.getContext('2d').getImageData` is available,
|
|
397
|
-
*
|
|
403
|
+
* Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
|
|
398
404
|
* @link https://github.com/nodeca/pica/blob/master/lib/utils.js
|
|
399
405
|
* @returns {boolean} Returns `true` if it is available, else `false`.
|
|
400
406
|
*/
|
|
@@ -439,7 +445,7 @@
|
|
|
439
445
|
/**
|
|
440
446
|
* Transform array buffer to Data URL.
|
|
441
447
|
* @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
|
|
442
|
-
* @param {string} mimeType - The
|
|
448
|
+
* @param {string} mimeType - The MIME type of the Data URL.
|
|
443
449
|
* @returns {string} The result Data URL.
|
|
444
450
|
*/
|
|
445
451
|
function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
@@ -687,11 +693,12 @@
|
|
|
687
693
|
*/
|
|
688
694
|
var Compressor = /*#__PURE__*/function () {
|
|
689
695
|
/**
|
|
690
|
-
*
|
|
691
|
-
* @param {File|Blob} file - The target image file
|
|
692
|
-
* @param {Object} [options] - The options
|
|
696
|
+
* Create a new Compressor.
|
|
697
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
698
|
+
* @param {Object} [options={}] - The configuration options.
|
|
693
699
|
*/
|
|
694
|
-
function Compressor(file
|
|
700
|
+
function Compressor(file) {
|
|
701
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
695
702
|
_classCallCheck(this, Compressor);
|
|
696
703
|
this.file = file;
|
|
697
704
|
this.exif = [];
|
|
@@ -707,7 +714,7 @@
|
|
|
707
714
|
var _this = this;
|
|
708
715
|
var file = this.file,
|
|
709
716
|
options = this.options;
|
|
710
|
-
if (!isBlob(file)) {
|
|
717
|
+
if (!isBlob$1(file)) {
|
|
711
718
|
this.fail(new Error('The first argument must be a File or Blob object.'));
|
|
712
719
|
return;
|
|
713
720
|
}
|
|
@@ -1078,14 +1085,15 @@
|
|
|
1078
1085
|
}
|
|
1079
1086
|
|
|
1080
1087
|
/**
|
|
1081
|
-
*
|
|
1082
|
-
* @
|
|
1088
|
+
* Creates a new Compressor instance.
|
|
1089
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
1090
|
+
* @param {Object} [options={}] - The configuration options.
|
|
1091
|
+
* @returns {Compressor} A new Compressor instance.
|
|
1083
1092
|
*/
|
|
1084
1093
|
}], [{
|
|
1085
|
-
key: "
|
|
1086
|
-
value: function
|
|
1087
|
-
|
|
1088
|
-
return Compressor;
|
|
1094
|
+
key: "create",
|
|
1095
|
+
value: function create(file, options) {
|
|
1096
|
+
return new Compressor(file, options);
|
|
1089
1097
|
}
|
|
1090
1098
|
|
|
1091
1099
|
/**
|
|
@@ -1097,6 +1105,17 @@
|
|
|
1097
1105
|
value: function setDefaults(options) {
|
|
1098
1106
|
_extends(DEFAULTS, options);
|
|
1099
1107
|
}
|
|
1108
|
+
|
|
1109
|
+
/**
|
|
1110
|
+
* Get the no conflict compressor class.
|
|
1111
|
+
* @returns {Compressor} The compressor class.
|
|
1112
|
+
*/
|
|
1113
|
+
}, {
|
|
1114
|
+
key: "noConflict",
|
|
1115
|
+
value: function noConflict() {
|
|
1116
|
+
window.Compressor = AnotherCompressor;
|
|
1117
|
+
return Compressor;
|
|
1118
|
+
}
|
|
1100
1119
|
}]);
|
|
1101
1120
|
}();
|
|
1102
1121
|
|
package/dist/compressor.min.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Compressor.js v1.
|
|
2
|
+
* Compressor.js v1.4.0
|
|
3
3
|
* https://fengyuanchen.github.io/compressorjs
|
|
4
4
|
*
|
|
5
5
|
* Copyright 2018-present Chen Fengyuan
|
|
6
6
|
* Released under the MIT license
|
|
7
7
|
*
|
|
8
|
-
* Date: 2026-
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
|
-
((e,t)=>{"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Compressor=t()})(this,function(){function a(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,o(i.key),i)}}function s(){return(s=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r,i=arguments[t];for(r in i)!{}.hasOwnProperty.call(i,r)||(e[r]=i[r])}return e}).apply(null,arguments)}function t(t,e){var r,i=Object.keys(t);return Object.getOwnPropertySymbols&&(r=Object.getOwnPropertySymbols(t),e&&(r=r.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),i.push.apply(i,r)),i}function n(i){for(var e=1;e<arguments.length;e++){var a=null!=arguments[e]?arguments[e]:{};e%2?t(Object(a),!0).forEach(function(e){var t,r;t=i,r=a[e=e],(e=o(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r}):Object.getOwnPropertyDescriptors?Object.defineProperties(i,Object.getOwnPropertyDescriptors(a)):t(Object(a)).forEach(function(e){Object.defineProperty(i,e,Object.getOwnPropertyDescriptor(a,e))})}return i}function o(e){e=((e,t)=>{if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);if("object"!=typeof(r=r.call(e,t||"default")))return r;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string");return"symbol"==typeof e?e:e+""}function x(e){return 0<e&&e<1/0}var e,r,l,c,h,u,f,d,i={exports:{}},O=(e=i,"undefined"!=typeof window&&(l=(r=window).HTMLCanvasElement&&r.HTMLCanvasElement.prototype,c=r.Blob&&(()=>{try{return Boolean(new Blob)}catch(e){return!1}})(),h=c&&r.Uint8Array&&(()=>{try{return 100===new Blob([new Uint8Array(100)]).size}catch(e){return!1}})(),u=r.BlobBuilder||r.WebKitBlobBuilder||r.MozBlobBuilder||r.MSBlobBuilder,f=/^data:((.*?)(;charset=.*?)?)(;base64)?,/,d=(c||u)&&r.atob&&r.ArrayBuffer&&r.Uint8Array&&function(e){var t,r,i,a,n,o=e.match(f);if(!o)throw new Error("invalid data URI");for(t=o[2]?o[1]:"text/plain"+(o[3]||";charset=US-ASCII"),n=!!o[4],e=e.slice(o[0].length),r=(n?atob:decodeURIComponent)(e),o=new ArrayBuffer(r.length),i=new Uint8Array(o),a=0;a<r.length;a+=1)i[a]=r.charCodeAt(a);return c?new Blob([h?i:o],{type:t}):((n=new u).append(o),n.getBlob(t))},r.HTMLCanvasElement&&!l.toBlob&&(l.mozGetAsFile?l.toBlob=function(e,t,r){var i=this;setTimeout(function(){r&&l.toDataURL&&d?e(d(i.toDataURL(t,r))):e(i.mozGetAsFile("blob",t))})}:l.toDataURL&&d&&(l.msToBlob?l.toBlob=function(e,t,r){var i=this;setTimeout(function(){(t&&"image/png"!==t||r)&&l.toDataURL&&d?e(d(i.toDataURL(t,r))):e(i.msToBlob(t))})}:l.toBlob=function(e,t,r){var i=this;setTimeout(function(){e(d(i.toDataURL(t,r)))})})),e.exports?e.exports=d:r.dataURLtoBlob=d),i.exports),m={strict:!0,checkOrientation:!0,retainExif:!1,maxWidth:1/0,maxHeight:1/0,minWidth:0,minHeight:0,width:void 0,height:void 0,resize:"none",quality:.8,mimeType:"auto",convertTypes:["image/png"],convertSize:5e6,beforeDraw:null,drew:null,success:null,error:null},g="undefined"!=typeof window&&void 0!==window.document?window:{},p=Array.prototype.slice;function j(e){return Array.from?Array.from(e):p.call(e)}var b=/^image\/.+$/;function T(e){return b.test(e)}var y=String.fromCharCode;var w=g.btoa;function A(e,t){for(var r=[],i=new Uint8Array(e);0<i.length;)r.push(y.apply(null,j(i.subarray(0,8192)))),i=i.subarray(8192);return"data:".concat(t,";base64,").concat(w(r.join("")))}function v(e){var t,r,i,a,n,o,l,s=new DataView(e);try{if(255===s.getUint8(0)&&216===s.getUint8(1))for(var c=s.byteLength,h=2;h+1<c;){if(255===s.getUint8(h)&&225===s.getUint8(h+1)){r=h;break}h+=1}if(i=r&&(a=r+10,"Exif"===((e,t,r)=>{var i,a="";for(r+=t,i=t;i<r;i+=1)a+=y(e.getUint8(i));return a})(s,r+4,4))&&((l=18761===(n=s.getUint16(a)))||19789===n)&&42===s.getUint16(a+2,l)&&8<=(o=s.getUint32(a+4,l))?a+o:i)for(var u,f=s.getUint16(i,l),d=0;d<f;d+=1)if(u=i+12*d+2,274===s.getUint16(u,l)){u+=8,t=s.getUint16(u,l),s.setUint16(u,1,l);break}}catch(e){t=1}return t}var B=/\.\d*(?:0|9){12}\d*$/;function R(e,t){t=1<arguments.length&&void 0!==t?t:1e11;return B.test(e)?Math.round(e*t)/t:e}function E(e,t){var r,i=e.aspectRatio,a=e.height,e=e.width,t=1<arguments.length&&void 0!==t?t:"none",n=x(e),o=x(a);return n&&o?(r=a*i,("contain"===t||"none"===t)&&e<r||"cover"===t&&r<e?a=e/i:e=a*i):n?a=e/i:o&&(e=a*i),{width:e,height:a}}var U=g.ArrayBuffer,k=g.FileReader,D=g.URL||g.webkitURL,M=/\.\w+$/,L=g.Compressor;return(()=>{function r(e,t){if(!(this instanceof r))throw new TypeError("Cannot call a class as a function");this.file=e,this.exif=[],this.image=new Image,this.options=n(n({},m),t),this.aborted=!1,this.result=null,this.init()}return e=r,i=[{key:"noConflict",value:function(){return window.Compressor=L,r}},{key:"setDefaults",value:function(e){s(m,e)}}],(t=[{key:"init",value:function(){var i,a,n,e,o=this,l=this.file,t=this.options;e=l,"undefined"!=typeof Blob&&(e instanceof Blob||"[object Blob]"===Object.prototype.toString.call(e))?T(i=l.type)?D&&k?(U||(t.checkOrientation=!1,t.retainExif=!1),a=(e="image/jpeg"===i)&&t.checkOrientation,n=e&&t.retainExif,!D||a||n?(e=new k,(this.reader=e).onload=function(e){var e=e.target.result,t={},r=1;a&&1<(r=v(e))&&s(t,(e=>{var t=0,r=1,i=1;switch(e){case 2:r=-1;break;case 3:t=-180;break;case 4:i=-1;break;case 5:t=90,i=-1;break;case 6:t=90;break;case 7:t=90,r=-1;break;case 8:t=-90}return{rotate:t,scaleX:r,scaleY:i}})(r)),n&&(o.exif=(e=>{for(var t=j(new Uint8Array(e)),r=t.length,i=[],a=0;a+3<r;){var n=t[a],o=t[a+1];if(255===n&&218===o)break;255===n&&216===o?a+=2:(n=a+(256*t[a+2]+t[a+3])+2,o=t.slice(a,n),i.push(o),a=n)}return i.reduce(function(e,t){return 255===t[0]&&225===t[1]?e.concat(t):e},[])})(e)),t.url=a||n?!D||1<r?A(e,i):D.createObjectURL(l):e,o.load(t)},e.onabort=function(){o.fail(new Error("Aborted to read the image with FileReader."))},e.onerror=function(){o.fail(new Error("Failed to read the image with FileReader."))},e.onloadend=function(){o.reader=null},a||n?e.readAsArrayBuffer(l):e.readAsDataURL(l)):this.load({url:D.createObjectURL(l)})):this.fail(new Error("The current browser does not support image compression.")):this.fail(new Error("The first argument must be an image File or Blob object.")):this.fail(new Error("The first argument must be a File or Blob object."))}},{key:"load",value:function(e){var t=this,r=this.file,i=this.image;i.onload=function(){(()=>{try{var e,t,r,i=document.createElement("canvas"),a=(i.width=2,i.height=1,i.getContext("2d"));if(a)return(e=a.createImageData(2,1)).data[0]=12,e.data[1]=23,e.data[2]=34,e.data[3]=255,e.data[4]=45,e.data[5]=56,e.data[6]=67,e.data[7]=255,a.putImageData(e,0,0),t=a.getImageData(0,0,2,1),r=[12,23,34,255,45,56,67,255],t.data.every(function(e,t){return e===r[t]})}catch(e){}})()?t.draw(n(n({},e),{},{naturalWidth:i.naturalWidth,naturalHeight:i.naturalHeight})):t.done({naturalWidth:i.naturalWidth,naturalHeight:i.naturalHeight,result:null})},i.onabort=function(){t.fail(new Error("Aborted to load the image."))},i.onerror=function(){t.fail(new Error("Failed to load the image."))},g.navigator&&/(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(g.navigator.userAgent)&&(i.crossOrigin="anonymous"),i.alt=r.name,i.src=e.url}},{key:"draw",value:function(e){var a=this,n=e.naturalWidth,o=e.naturalHeight,t=e.rotate,t=void 0===t?0:t,r=e.scaleX,r=void 0===r?1:r,e=e.scaleY,e=void 0===e?1:e,i=this.file,l=this.image,s=this.options,c=document.createElement("canvas"),h=c.getContext("2d"),u=Math.abs(t)%180==90,f=("contain"===s.resize||"cover"===s.resize)&&x(s.width)&&x(s.height),d=Math.max(s.maxWidth,0)||1/0,m=Math.max(s.maxHeight,0)||1/0,g=Math.max(s.minWidth,0)||0,p=Math.max(s.minHeight,0)||0,b=n/o,y=s.width,w=s.height,v=(u&&(d=(v=[m,d])[0],m=v[1],g=(v=[p,g])[0],p=v[1],y=(v=[w,y])[0],w=v[1]),E({aspectRatio:b=f?y/w:b,width:d,height:m},"contain")),v=(d=v.width,m=v.height,E({aspectRatio:b,width:g,height:p},"cover")),v=(g=v.width,p=v.height,w=f?(y=(v=E({aspectRatio:b,width:y,height:w},s.resize)).width,v.height):(y=void 0===(B=(v=E({aspectRatio:b,width:y,height:w})).width)?n:B,void 0===(B=v.height)?o:B),-(y=Math.floor(R(Math.min(Math.max(y,g),d))))/2),B=-(w=Math.floor(R(Math.min(Math.max(w,p),m))))/2,g=y,d=w,p=[],m=(f&&(0,m=(b=E({aspectRatio:b,width:m=n,height:f=o},{contain:"cover",cover:"contain"}[s.resize])).width,p.push((n-m)/2,(o-(f=b.height))/2,m,f)),p.push(v,B,g,d),u&&(y=(b=[w,y])[0],w=b[1]),c.width=y,c.height=w,T(s.mimeType)||(s.mimeType=i.type),"transparent"),U=(i.size>s.convertSize&&0<=s.convertTypes.indexOf(s.mimeType)&&(s.mimeType="image/jpeg"),"image/jpeg"===s.mimeType);h.fillStyle=m=U?"#fff":m,h.fillRect(0,0,y,w),s.beforeDraw&&s.beforeDraw.call(this,h,c),this.aborted||(h.save(),h.translate(y/2,w/2),h.rotate(t*Math.PI/180),h.scale(r,e),h.drawImage.apply(h,[l].concat(p)),h.restore(),s.drew&&s.drew.call(this,h,c),this.aborted)||(f=function(e){var i,t,r;a.aborted||(i=function(e){return a.done({naturalWidth:n,naturalHeight:o,result:e})},e&&U&&s.retainExif&&a.exif&&0<a.exif.length?(t=function(e){return i(O(A((e=e,t=a.exif,255!==(r=j(new Uint8Array(e)))[2]||224!==r[3]?e:(e=[255,216].concat(t,r.slice(4+(256*r[4]+r[5]))),new Uint8Array(e))),s.mimeType)));var t,r},e.arrayBuffer?e.arrayBuffer().then(t).catch(function(){a.fail(new Error("Failed to read the compressed image with Blob.arrayBuffer()."))}):(r=new k,(a.reader=r).onload=function(e){e=e.target;t(e.result)},r.onabort=function(){a.fail(new Error("Aborted to read the compressed image with FileReader."))},r.onerror=function(){a.fail(new Error("Failed to read the compressed image with FileReader."))},r.onloadend=function(){a.reader=null},r.readAsArrayBuffer(e))):i(e))},c.toBlob?c.toBlob(f,s.mimeType,s.quality):f(O(c.toDataURL(s.mimeType,s.quality))))}},{key:"done",value:function(t){var e=t.naturalWidth,r=t.naturalHeight,t=t.result,i=this.file,a=this.image,n=this.options;if(D&&0===a.src.indexOf("blob:")&&D.revokeObjectURL(a.src),t)if(n.strict&&!n.retainExif&&t.size>i.size&&n.mimeType===i.type&&!(n.width>e||n.height>r||n.minWidth>e||n.minHeight>r||n.maxWidth<e||n.maxHeight<r))t=i;else{a=i.name;a&&t.type!==i.type&&(a=a.replace(M,(e=T(e=t.type)?e.substr(6):"",".".concat(e="jpeg"===e?"jpg":e))));try{t=new File([t],a,{type:t.type})}catch(e){r=new Date;t.name=a,t.lastModified=r.getTime(),t.lastModifiedDate=r}}else t=i;this.result=t,n.success&&n.success.call(this,t)}},{key:"fail",value:function(e){var t=this.options;if(!t.error)throw e;t.error.call(this,e)}},{key:"abort",value:function(){this.aborted||(this.aborted=!0,this.reader?this.reader.abort():this.image.complete?this.fail(new Error("The compression process has been aborted.")):(this.image.onload=null,this.image.onabort()))}}])&&a(e.prototype,t),i&&a(e,i),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,t,i})()});
|
|
10
|
+
((e,t)=>{"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Compressor=t()})(this,function(){function a(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,o(a.key),a)}}function s(){return(s=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r,a=arguments[t];for(r in a)!{}.hasOwnProperty.call(a,r)||(e[r]=a[r])}return e}).apply(null,arguments)}function t(t,e){var r,a=Object.keys(t);return Object.getOwnPropertySymbols&&(r=Object.getOwnPropertySymbols(t),e&&(r=r.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),a.push.apply(a,r)),a}function n(a){for(var e=1;e<arguments.length;e++){var i=null!=arguments[e]?arguments[e]:{};e%2?t(Object(i),!0).forEach(function(e){var t,r;t=a,r=i[e=e],(e=o(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(i)):t(Object(i)).forEach(function(e){Object.defineProperty(a,e,Object.getOwnPropertyDescriptor(i,e))})}return a}function o(e){e=((e,t)=>{if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);if("object"!=typeof(r=r.call(e,t||"default")))return r;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string");return"symbol"==typeof e?e:e+""}function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var r,i,l,c,u,h,f,d,m={exports:{}};r=m,"undefined"!=typeof window&&(l=(i=window).HTMLCanvasElement&&i.HTMLCanvasElement.prototype,c=i.Blob&&(()=>{try{return Boolean(new Blob)}catch(e){return!1}})(),u=c&&i.Uint8Array&&(()=>{try{return 100===new Blob([new Uint8Array(100)]).size}catch(e){return!1}})(),h=i.BlobBuilder||i.WebKitBlobBuilder||i.MozBlobBuilder||i.MSBlobBuilder,f=/^data:((.*?)(;charset=.*?)?)(;base64)?,/,d=(c||h)&&i.atob&&i.ArrayBuffer&&i.Uint8Array&&function(e){var t,r,a,i,n,o=e.match(f);if(!o)throw new Error("invalid data URI");for(t=o[2]?o[1]:"text/plain"+(o[3]||";charset=US-ASCII"),n=!!o[4],e=e.slice(o[0].length),r=(n?atob:decodeURIComponent)(e),o=new ArrayBuffer(r.length),a=new Uint8Array(o),i=0;i<r.length;i+=1)a[i]=r.charCodeAt(i);return c?new Blob([u?a:o],{type:t}):((n=new h).append(o),n.getBlob(t))},i.HTMLCanvasElement&&!l.toBlob&&(l.mozGetAsFile?l.toBlob=function(e,t,r){var a=this;setTimeout(function(){r&&l.toDataURL&&d?e(d(a.toDataURL(t,r))):e(a.mozGetAsFile("blob",t))})}:l.toDataURL&&d&&(l.msToBlob?l.toBlob=function(e,t,r){var a=this;setTimeout(function(){(t&&"image/png"!==t||r)&&l.toDataURL&&d?e(d(a.toDataURL(t,r))):e(a.msToBlob(t))})}:l.toBlob=function(e,t,r){var a=this;setTimeout(function(){e(d(a.toDataURL(t,r)))})})),r.exports?r.exports=d:i.dataURLtoBlob=d);function O(e){return 0<e&&e<1/0}var x=e(m.exports),p=e(function(e){return"undefined"!=typeof Blob&&(e instanceof Blob||"[object Blob]"===Object.prototype.toString.call(e))}),g={strict:!0,checkOrientation:!0,retainExif:!1,maxWidth:1/0,maxHeight:1/0,minWidth:0,minHeight:0,width:void 0,height:void 0,resize:"none",quality:.8,mimeType:"auto",convertTypes:["image/png"],convertSize:5e6,beforeDraw:null,drew:null,success:null,error:null},b="undefined"!=typeof window&&void 0!==window.document?window:{},y=Array.prototype.slice;function j(e){return Array.from?Array.from(e):y.call(e)}var w=/^image\/.+$/;function T(e){return w.test(e)}var v=String.fromCharCode;var B=b.btoa;function A(e,t){for(var r=[],a=new Uint8Array(e);0<a.length;)r.push(v.apply(null,j(a.subarray(0,8192)))),a=a.subarray(8192);return"data:".concat(t,";base64,").concat(B(r.join("")))}function U(e){var t,r,a,i,n,o,l,s=new DataView(e);try{if(255===s.getUint8(0)&&216===s.getUint8(1))for(var c=s.byteLength,u=2;u+1<c;){if(255===s.getUint8(u)&&225===s.getUint8(u+1)){r=u;break}u+=1}if(a=r&&(i=r+10,"Exif"===((e,t,r)=>{var a,i="";for(r+=t,a=t;a<r;a+=1)i+=v(e.getUint8(a));return i})(s,r+4,4))&&((l=18761===(n=s.getUint16(i)))||19789===n)&&42===s.getUint16(i+2,l)&&8<=(o=s.getUint32(i+4,l))?i+o:a)for(var h,f=s.getUint16(a,l),d=0;d<f;d+=1)if(h=a+12*d+2,274===s.getUint16(h,l)){h+=8,t=s.getUint16(h,l),s.setUint16(h,1,l);break}}catch(e){t=1}return t}var R=/\.\d*(?:0|9){12}\d*$/;function E(e,t){t=1<arguments.length&&void 0!==t?t:1e11;return R.test(e)?Math.round(e*t)/t:e}function k(e,t){var r,a=e.aspectRatio,i=e.height,e=e.width,t=1<arguments.length&&void 0!==t?t:"none",n=O(e),o=O(i);return n&&o?(r=i*a,("contain"===t||"none"===t)&&e<r||"cover"===t&&r<e?i=e/a:e=i*a):n?i=e/a:o&&(e=i*a),{width:e,height:i}}var D=b.ArrayBuffer,M=b.FileReader,P=b.URL||b.webkitURL,L=/\.\w+$/,H=b.Compressor;return(()=>{function i(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=this,a=i;if(!(r instanceof a))throw new TypeError("Cannot call a class as a function");this.file=e,this.exif=[],this.image=new Image,this.options=n(n({},g),t),this.aborted=!1,this.result=null,this.init()}return e=i,r=[{key:"create",value:function(e,t){return new i(e,t)}},{key:"setDefaults",value:function(e){s(g,e)}},{key:"noConflict",value:function(){return window.Compressor=H,i}}],(t=[{key:"init",value:function(){var a,i,n,e,o=this,l=this.file,t=this.options;p(l)?T(a=l.type)?P&&M?(D||(t.checkOrientation=!1,t.retainExif=!1),i=(e="image/jpeg"===a)&&t.checkOrientation,n=e&&t.retainExif,!P||i||n?(e=new M,(this.reader=e).onload=function(e){var e=e.target.result,t={},r=1;i&&1<(r=U(e))&&s(t,(e=>{var t=0,r=1,a=1;switch(e){case 2:r=-1;break;case 3:t=-180;break;case 4:a=-1;break;case 5:t=90,a=-1;break;case 6:t=90;break;case 7:t=90,r=-1;break;case 8:t=-90}return{rotate:t,scaleX:r,scaleY:a}})(r)),n&&(o.exif=(e=>{for(var t=j(new Uint8Array(e)),r=t.length,a=[],i=0;i+3<r;){var n=t[i],o=t[i+1];if(255===n&&218===o)break;255===n&&216===o?i+=2:(n=i+(256*t[i+2]+t[i+3])+2,o=t.slice(i,n),a.push(o),i=n)}return a.reduce(function(e,t){return 255===t[0]&&225===t[1]?e.concat(t):e},[])})(e)),t.url=i||n?!P||1<r?A(e,a):P.createObjectURL(l):e,o.load(t)},e.onabort=function(){o.fail(new Error("Aborted to read the image with FileReader."))},e.onerror=function(){o.fail(new Error("Failed to read the image with FileReader."))},e.onloadend=function(){o.reader=null},i||n?e.readAsArrayBuffer(l):e.readAsDataURL(l)):this.load({url:P.createObjectURL(l)})):this.fail(new Error("The current browser does not support image compression.")):this.fail(new Error("The first argument must be an image File or Blob object.")):this.fail(new Error("The first argument must be a File or Blob object."))}},{key:"load",value:function(e){var t=this,r=this.file,a=this.image;a.onload=function(){(()=>{try{var e,t,r,a=document.createElement("canvas"),i=(a.width=2,a.height=1,a.getContext("2d"));if(i)return(e=i.createImageData(2,1)).data[0]=12,e.data[1]=23,e.data[2]=34,e.data[3]=255,e.data[4]=45,e.data[5]=56,e.data[6]=67,e.data[7]=255,i.putImageData(e,0,0),t=i.getImageData(0,0,2,1),r=[12,23,34,255,45,56,67,255],t.data.every(function(e,t){return e===r[t]})}catch(e){}})()?t.draw(n(n({},e),{},{naturalWidth:a.naturalWidth,naturalHeight:a.naturalHeight})):t.done({naturalWidth:a.naturalWidth,naturalHeight:a.naturalHeight,result:null})},a.onabort=function(){t.fail(new Error("Aborted to load the image."))},a.onerror=function(){t.fail(new Error("Failed to load the image."))},b.navigator&&/(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(b.navigator.userAgent)&&(a.crossOrigin="anonymous"),a.alt=r.name,a.src=e.url}},{key:"draw",value:function(e){var i=this,n=e.naturalWidth,o=e.naturalHeight,t=e.rotate,t=void 0===t?0:t,r=e.scaleX,r=void 0===r?1:r,e=e.scaleY,e=void 0===e?1:e,a=this.file,l=this.image,s=this.options,c=document.createElement("canvas"),u=c.getContext("2d"),h=Math.abs(t)%180==90,f=("contain"===s.resize||"cover"===s.resize)&&O(s.width)&&O(s.height),d=Math.max(s.maxWidth,0)||1/0,m=Math.max(s.maxHeight,0)||1/0,p=Math.max(s.minWidth,0)||0,g=Math.max(s.minHeight,0)||0,b=n/o,y=s.width,w=s.height,v=(h&&(d=(v=[m,d])[0],m=v[1],p=(v=[g,p])[0],g=v[1],y=(v=[w,y])[0],w=v[1]),k({aspectRatio:b=f?y/w:b,width:d,height:m},"contain")),v=(d=v.width,m=v.height,k({aspectRatio:b,width:p,height:g},"cover")),v=(p=v.width,g=v.height,w=f?(y=(v=k({aspectRatio:b,width:y,height:w},s.resize)).width,v.height):(y=void 0===(B=(v=k({aspectRatio:b,width:y,height:w})).width)?n:B,void 0===(B=v.height)?o:B),-(y=Math.floor(E(Math.min(Math.max(y,p),d))))/2),B=-(w=Math.floor(E(Math.min(Math.max(w,g),m))))/2,p=y,d=w,g=[],m=(f&&(0,m=(b=k({aspectRatio:b,width:m=n,height:f=o},{contain:"cover",cover:"contain"}[s.resize])).width,g.push((n-m)/2,(o-(f=b.height))/2,m,f)),g.push(v,B,p,d),h&&(y=(b=[w,y])[0],w=b[1]),c.width=y,c.height=w,T(s.mimeType)||(s.mimeType=a.type),"transparent"),U=(a.size>s.convertSize&&0<=s.convertTypes.indexOf(s.mimeType)&&(s.mimeType="image/jpeg"),"image/jpeg"===s.mimeType);u.fillStyle=m=U?"#fff":m,u.fillRect(0,0,y,w),s.beforeDraw&&s.beforeDraw.call(this,u,c),this.aborted||(u.save(),u.translate(y/2,w/2),u.rotate(t*Math.PI/180),u.scale(r,e),u.drawImage.apply(u,[l].concat(g)),u.restore(),s.drew&&s.drew.call(this,u,c),this.aborted)||(f=function(e){var a,t,r;i.aborted||(a=function(e){return i.done({naturalWidth:n,naturalHeight:o,result:e})},e&&U&&s.retainExif&&i.exif&&0<i.exif.length?(t=function(e){return a(x(A((e=e,t=i.exif,255!==(r=j(new Uint8Array(e)))[2]||224!==r[3]?e:(e=[255,216].concat(t,r.slice(4+(256*r[4]+r[5]))),new Uint8Array(e))),s.mimeType)));var t,r},e.arrayBuffer?e.arrayBuffer().then(t).catch(function(){i.fail(new Error("Failed to read the compressed image with Blob.arrayBuffer()."))}):(r=new M,(i.reader=r).onload=function(e){e=e.target;t(e.result)},r.onabort=function(){i.fail(new Error("Aborted to read the compressed image with FileReader."))},r.onerror=function(){i.fail(new Error("Failed to read the compressed image with FileReader."))},r.onloadend=function(){i.reader=null},r.readAsArrayBuffer(e))):a(e))},c.toBlob?c.toBlob(f,s.mimeType,s.quality):f(x(c.toDataURL(s.mimeType,s.quality))))}},{key:"done",value:function(t){var e=t.naturalWidth,r=t.naturalHeight,t=t.result,a=this.file,i=this.image,n=this.options;if(P&&0===i.src.indexOf("blob:")&&P.revokeObjectURL(i.src),t)if(n.strict&&!n.retainExif&&t.size>a.size&&n.mimeType===a.type&&!(n.width>e||n.height>r||n.minWidth>e||n.minHeight>r||n.maxWidth<e||n.maxHeight<r))t=a;else{i=a.name;i&&t.type!==a.type&&(i=i.replace(L,(e=T(e=t.type)?e.substr(6):"",".".concat(e="jpeg"===e?"jpg":e))));try{t=new File([t],i,{type:t.type})}catch(e){r=new Date;t.name=i,t.lastModified=r.getTime(),t.lastModifiedDate=r}}else t=a;this.result=t,n.success&&n.success.call(this,t)}},{key:"fail",value:function(e){var t=this.options;if(!t.error)throw e;t.error.call(this,e)}},{key:"abort",value:function(){this.aborted||(this.aborted=!0,this.reader?this.reader.abort():this.image.complete?this.fail(new Error("The compression process has been aborted.")):(this.image.onload=null,this.image.onabort()))}}])&&a(e.prototype,t),r&&a(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,t,r})()});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compressorjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "JavaScript image compressor.",
|
|
5
5
|
"main": "dist/compressor.common.js",
|
|
6
6
|
"module": "dist/compressor.esm.js",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"lint": "npm run lint:js && npm run lint:css",
|
|
20
20
|
"lint:css": "stylelint docs/**/*.css --fix",
|
|
21
21
|
"lint:js": "eslint src test *.js --fix",
|
|
22
|
-
"prepare": "
|
|
22
|
+
"prepare": "simple-git-hooks",
|
|
23
23
|
"release": "npm run clean && npm run lint && npm run build && npm run compress && npm test",
|
|
24
24
|
"start": "npm run dev",
|
|
25
25
|
"test": "cross-env NODE_ENV=test karma start test/karma.conf.js"
|
|
@@ -53,16 +53,16 @@
|
|
|
53
53
|
"is-blob": "^2.1.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@babel/core": "^7.29.
|
|
57
|
-
"@babel/plugin-transform-object-assign": "^7.
|
|
58
|
-
"@babel/preset-env": "^7.29.
|
|
56
|
+
"@babel/core": "^7.29.7",
|
|
57
|
+
"@babel/plugin-transform-object-assign": "^7.29.7",
|
|
58
|
+
"@babel/preset-env": "^7.29.7",
|
|
59
59
|
"@commitlint/cli": "^17.8.1",
|
|
60
60
|
"@commitlint/config-conventional": "^17.8.1",
|
|
61
|
-
"@rollup/plugin-babel": "^
|
|
62
|
-
"@rollup/plugin-commonjs": "^
|
|
63
|
-
"@rollup/plugin-node-resolve": "^
|
|
64
|
-
"@rollup/plugin-replace": "^
|
|
65
|
-
"babel-plugin-istanbul": "^
|
|
61
|
+
"@rollup/plugin-babel": "^6.1.0",
|
|
62
|
+
"@rollup/plugin-commonjs": "^26.0.3",
|
|
63
|
+
"@rollup/plugin-node-resolve": "^15.3.1",
|
|
64
|
+
"@rollup/plugin-replace": "^6.0.3",
|
|
65
|
+
"babel-plugin-istanbul": "^7.0.1",
|
|
66
66
|
"chai": "^4.5.0",
|
|
67
67
|
"change-case": "^4.1.2",
|
|
68
68
|
"codecov": "^3.8.3",
|
|
@@ -72,7 +72,6 @@
|
|
|
72
72
|
"eslint": "^8.57.1",
|
|
73
73
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
74
74
|
"eslint-plugin-import": "^2.32.0",
|
|
75
|
-
"husky": "^8.0.3",
|
|
76
75
|
"karma": "^6.4.4",
|
|
77
76
|
"karma-chai": "^0.1.0",
|
|
78
77
|
"karma-chrome-launcher": "^3.2.0",
|
|
@@ -83,7 +82,8 @@
|
|
|
83
82
|
"lint-staged": "^13.3.0",
|
|
84
83
|
"mocha": "^10.8.2",
|
|
85
84
|
"puppeteer": "^19.11.1",
|
|
86
|
-
"rollup": "^
|
|
85
|
+
"rollup": "^3.30.0",
|
|
86
|
+
"simple-git-hooks": "^2.13.1",
|
|
87
87
|
"stylelint": "^15.11.0",
|
|
88
88
|
"stylelint-config-standard": "^30.0.1",
|
|
89
89
|
"stylelint-order": "^6.0.4",
|
package/src/defaults.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
/**
|
|
3
|
-
* Indicates
|
|
3
|
+
* Indicates whether to output the original image instead of the compressed one
|
|
4
4
|
* when the size of the compressed image is greater than the original one's
|
|
5
5
|
* @type {boolean}
|
|
6
6
|
*/
|
|
7
7
|
strict: true,
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* Indicates
|
|
10
|
+
* Indicates whether to read the image's Exif Orientation information,
|
|
11
11
|
* and then rotate or flip the image automatically.
|
|
12
12
|
* @type {boolean}
|
|
13
13
|
*/
|
|
14
14
|
checkOrientation: true,
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Indicates
|
|
17
|
+
* Indicates whether to retain the image's Exif information after compression.
|
|
18
18
|
* @type {boolean}
|
|
19
19
|
*/
|
|
20
20
|
retainExif: false,
|
|
@@ -83,7 +83,7 @@ export default {
|
|
|
83
83
|
/**
|
|
84
84
|
* Files whose file type is included in this list,
|
|
85
85
|
* and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
86
|
-
* @type {string
|
|
86
|
+
* @type {string|string[]}
|
|
87
87
|
*/
|
|
88
88
|
convertTypes: ['image/png'],
|
|
89
89
|
|
|
@@ -95,9 +95,9 @@ export default {
|
|
|
95
95
|
convertSize: 5000000,
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* The hook function to execute before
|
|
98
|
+
* The hook function to execute before drawing the image into the canvas for compression.
|
|
99
99
|
* @type {Function}
|
|
100
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
100
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
101
101
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
102
102
|
* @example
|
|
103
103
|
* function (context, canvas) {
|
|
@@ -107,9 +107,9 @@ export default {
|
|
|
107
107
|
beforeDraw: null,
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
* The hook function to execute after
|
|
110
|
+
* The hook function to execute after drawing the image into the canvas for compression.
|
|
111
111
|
* @type {Function}
|
|
112
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
112
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
113
113
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
114
114
|
* @example
|
|
115
115
|
* function (context, canvas) {
|
|
@@ -119,7 +119,7 @@ export default {
|
|
|
119
119
|
drew: null,
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
|
-
* The hook function to execute when
|
|
122
|
+
* The hook function to execute when the image is compressed successfully.
|
|
123
123
|
* @type {Function}
|
|
124
124
|
* @param {File} file - The compressed image File object.
|
|
125
125
|
* @example
|
package/src/index.js
CHANGED
|
@@ -29,11 +29,11 @@ const AnotherCompressor = WINDOW.Compressor;
|
|
|
29
29
|
*/
|
|
30
30
|
export default class Compressor {
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
* @param {File|Blob} file - The target image file
|
|
34
|
-
* @param {Object} [options] - The options
|
|
32
|
+
* Create a new Compressor.
|
|
33
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
34
|
+
* @param {Object} [options={}] - The configuration options.
|
|
35
35
|
*/
|
|
36
|
-
constructor(file, options) {
|
|
36
|
+
constructor(file, options = {}) {
|
|
37
37
|
this.file = file;
|
|
38
38
|
this.exif = [];
|
|
39
39
|
this.image = new Image();
|
|
@@ -451,12 +451,13 @@ export default class Compressor {
|
|
|
451
451
|
}
|
|
452
452
|
|
|
453
453
|
/**
|
|
454
|
-
*
|
|
455
|
-
* @
|
|
454
|
+
* Creates a new Compressor instance.
|
|
455
|
+
* @param {File|Blob} file - The target image file to compress.
|
|
456
|
+
* @param {Object} [options={}] - The configuration options.
|
|
457
|
+
* @returns {Compressor} A new Compressor instance.
|
|
456
458
|
*/
|
|
457
|
-
static
|
|
458
|
-
|
|
459
|
-
return Compressor;
|
|
459
|
+
static create(file, options) {
|
|
460
|
+
return new Compressor(file, options);
|
|
460
461
|
}
|
|
461
462
|
|
|
462
463
|
/**
|
|
@@ -466,4 +467,13 @@ export default class Compressor {
|
|
|
466
467
|
static setDefaults(options) {
|
|
467
468
|
Object.assign(DEFAULTS, options);
|
|
468
469
|
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Get the no conflict compressor class.
|
|
473
|
+
* @returns {Compressor} The compressor class.
|
|
474
|
+
*/
|
|
475
|
+
static noConflict() {
|
|
476
|
+
window.Compressor = AnotherCompressor;
|
|
477
|
+
return Compressor;
|
|
478
|
+
}
|
|
469
479
|
}
|
package/src/utilities.js
CHANGED
|
@@ -21,9 +21,9 @@ export function toArray(value) {
|
|
|
21
21
|
const REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* Check if the given value is
|
|
24
|
+
* Check if the given value is an image MIME type.
|
|
25
25
|
* @param {*} value - The value to check.
|
|
26
|
-
* @returns {boolean} Returns `true` if the given is
|
|
26
|
+
* @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
|
|
27
27
|
*/
|
|
28
28
|
export function isImageType(value) {
|
|
29
29
|
return REGEXP_IMAGE_TYPE.test(value);
|
|
@@ -32,7 +32,7 @@ export function isImageType(value) {
|
|
|
32
32
|
/**
|
|
33
33
|
* Convert image type to extension.
|
|
34
34
|
* @param {string} value - The image type to convert.
|
|
35
|
-
* @returns {
|
|
35
|
+
* @returns {string} Returns the image extension.
|
|
36
36
|
*/
|
|
37
37
|
export function imageTypeToExtension(value) {
|
|
38
38
|
let extension = isImageType(value) ? value.substr(6) : '';
|
|
@@ -68,7 +68,7 @@ export function getStringFromCharCode(dataView, start, length) {
|
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* Check if `canvas.getContext('2d').getImageData` is available,
|
|
71
|
-
*
|
|
71
|
+
* Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
|
|
72
72
|
* @link https://github.com/nodeca/pica/blob/master/lib/utils.js
|
|
73
73
|
* @returns {boolean} Returns `true` if it is available, else `false`.
|
|
74
74
|
*/
|
|
@@ -117,7 +117,7 @@ const { btoa } = WINDOW;
|
|
|
117
117
|
/**
|
|
118
118
|
* Transform array buffer to Data URL.
|
|
119
119
|
* @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
|
|
120
|
-
* @param {string} mimeType - The
|
|
120
|
+
* @param {string} mimeType - The MIME type of the Data URL.
|
|
121
121
|
* @returns {string} The result Data URL.
|
|
122
122
|
*/
|
|
123
123
|
export function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
declare namespace Compressor {
|
|
2
|
-
|
|
2
|
+
type ResizeMode = 'contain' | 'cover' | 'none';
|
|
3
|
+
|
|
4
|
+
interface Options {
|
|
3
5
|
strict?: boolean;
|
|
4
6
|
checkOrientation?: boolean;
|
|
5
7
|
retainExif?: boolean;
|
|
@@ -9,22 +11,23 @@ declare namespace Compressor {
|
|
|
9
11
|
minHeight?: number;
|
|
10
12
|
width?: number;
|
|
11
13
|
height?: number;
|
|
12
|
-
resize?:
|
|
14
|
+
resize?: ResizeMode;
|
|
13
15
|
quality?: number;
|
|
14
16
|
mimeType?: string;
|
|
15
17
|
convertTypes?: string | string[];
|
|
16
18
|
convertSize?: number;
|
|
17
|
-
beforeDraw
|
|
18
|
-
drew
|
|
19
|
-
success
|
|
20
|
-
error
|
|
19
|
+
beforeDraw?: ((this: Compressor, context: CanvasRenderingContext2D, canvas: HTMLCanvasElement) => void) | null;
|
|
20
|
+
drew?: ((this: Compressor, context: CanvasRenderingContext2D, canvas: HTMLCanvasElement) => void) | null;
|
|
21
|
+
success?: ((this: Compressor, file: File) => void) | null;
|
|
22
|
+
error?: ((this: Compressor, error: Error) => void) | null;
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
declare class Compressor {
|
|
25
27
|
constructor(file: File | Blob, options?: Compressor.Options);
|
|
26
28
|
abort(): void;
|
|
27
|
-
static
|
|
29
|
+
static create(file: File | Blob, options?: Compressor.Options): Compressor;
|
|
30
|
+
static noConflict(): typeof Compressor;
|
|
28
31
|
static setDefaults(options: Compressor.Options): void;
|
|
29
32
|
}
|
|
30
33
|
|