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
|
@@ -1,99 +1,84 @@
|
|
|
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:
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
if (Object.getOwnPropertySymbols) {
|
|
16
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
17
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
18
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
19
|
-
})), keys.push.apply(keys, symbols);
|
|
20
|
-
}
|
|
21
|
-
return keys;
|
|
13
|
+
function _classCallCheck(a, n) {
|
|
14
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
22
15
|
}
|
|
23
|
-
function
|
|
24
|
-
for (var
|
|
25
|
-
var
|
|
26
|
-
|
|
27
|
-
_defineProperty(target, key, source[key]);
|
|
28
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
29
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
30
|
-
});
|
|
16
|
+
function _defineProperties(e, r) {
|
|
17
|
+
for (var t = 0; t < r.length; t++) {
|
|
18
|
+
var o = r[t];
|
|
19
|
+
o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
31
20
|
}
|
|
32
|
-
return target;
|
|
33
21
|
}
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
22
|
+
function _createClass(e, r, t) {
|
|
23
|
+
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
|
|
24
|
+
writable: !1
|
|
25
|
+
}), e;
|
|
38
26
|
}
|
|
39
|
-
function
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
27
|
+
function _defineProperty(e, r, t) {
|
|
28
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
29
|
+
value: t,
|
|
30
|
+
enumerable: !0,
|
|
31
|
+
configurable: !0,
|
|
32
|
+
writable: !0
|
|
33
|
+
}) : e[r] = t, e;
|
|
47
34
|
}
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
35
|
+
function _extends() {
|
|
36
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
37
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
38
|
+
var t = arguments[e];
|
|
39
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
40
|
+
}
|
|
41
|
+
return n;
|
|
42
|
+
}, _extends.apply(null, arguments);
|
|
55
43
|
}
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
Object.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
writable: true
|
|
64
|
-
});
|
|
65
|
-
} else {
|
|
66
|
-
obj[key] = value;
|
|
44
|
+
function ownKeys(e, r) {
|
|
45
|
+
var t = Object.keys(e);
|
|
46
|
+
if (Object.getOwnPropertySymbols) {
|
|
47
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
48
|
+
r && (o = o.filter(function (r) {
|
|
49
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
50
|
+
})), t.push.apply(t, o);
|
|
67
51
|
}
|
|
68
|
-
return
|
|
52
|
+
return t;
|
|
69
53
|
}
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return target;
|
|
81
|
-
};
|
|
82
|
-
return _extends.apply(this, arguments);
|
|
54
|
+
function _objectSpread2(e) {
|
|
55
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
56
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
57
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
58
|
+
_defineProperty(e, r, t[r]);
|
|
59
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
60
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return e;
|
|
83
64
|
}
|
|
84
|
-
function _toPrimitive(
|
|
85
|
-
if (
|
|
86
|
-
var
|
|
87
|
-
if (
|
|
88
|
-
var
|
|
89
|
-
if (
|
|
65
|
+
function _toPrimitive(t, r) {
|
|
66
|
+
if ("object" != typeof t || !t) return t;
|
|
67
|
+
var e = t[Symbol.toPrimitive];
|
|
68
|
+
if (void 0 !== e) {
|
|
69
|
+
var i = e.call(t, r || "default");
|
|
70
|
+
if ("object" != typeof i) return i;
|
|
90
71
|
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
91
72
|
}
|
|
92
|
-
return (
|
|
73
|
+
return ("string" === r ? String : Number)(t);
|
|
93
74
|
}
|
|
94
|
-
function _toPropertyKey(
|
|
95
|
-
var
|
|
96
|
-
return
|
|
75
|
+
function _toPropertyKey(t) {
|
|
76
|
+
var i = _toPrimitive(t, "string");
|
|
77
|
+
return "symbol" == typeof i ? i : i + "";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getDefaultExportFromCjs (x) {
|
|
81
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
97
82
|
}
|
|
98
83
|
|
|
99
84
|
var canvasToBlob = {exports: {}};
|
|
@@ -209,7 +194,8 @@ var canvasToBlob = {exports: {}};
|
|
|
209
194
|
}
|
|
210
195
|
})(window);
|
|
211
196
|
})(canvasToBlob);
|
|
212
|
-
var
|
|
197
|
+
var canvasToBlobExports = canvasToBlob.exports;
|
|
198
|
+
var toBlob = /*@__PURE__*/getDefaultExportFromCjs(canvasToBlobExports);
|
|
213
199
|
|
|
214
200
|
var isBlob = function isBlob(value) {
|
|
215
201
|
if (typeof Blob === 'undefined') {
|
|
@@ -217,22 +203,23 @@ var isBlob = function isBlob(value) {
|
|
|
217
203
|
}
|
|
218
204
|
return value instanceof Blob || Object.prototype.toString.call(value) === '[object Blob]';
|
|
219
205
|
};
|
|
206
|
+
var isBlob$1 = /*@__PURE__*/getDefaultExportFromCjs(isBlob);
|
|
220
207
|
|
|
221
208
|
var DEFAULTS = {
|
|
222
209
|
/**
|
|
223
|
-
* Indicates
|
|
210
|
+
* Indicates whether to output the original image instead of the compressed one
|
|
224
211
|
* when the size of the compressed image is greater than the original one's
|
|
225
212
|
* @type {boolean}
|
|
226
213
|
*/
|
|
227
214
|
strict: true,
|
|
228
215
|
/**
|
|
229
|
-
* Indicates
|
|
216
|
+
* Indicates whether to read the image's Exif Orientation information,
|
|
230
217
|
* and then rotate or flip the image automatically.
|
|
231
218
|
* @type {boolean}
|
|
232
219
|
*/
|
|
233
220
|
checkOrientation: true,
|
|
234
221
|
/**
|
|
235
|
-
* Indicates
|
|
222
|
+
* Indicates whether to retain the image's Exif information after compression.
|
|
236
223
|
* @type {boolean}
|
|
237
224
|
*/
|
|
238
225
|
retainExif: false,
|
|
@@ -291,7 +278,7 @@ var DEFAULTS = {
|
|
|
291
278
|
/**
|
|
292
279
|
* Files whose file type is included in this list,
|
|
293
280
|
* and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
294
|
-
* @type {string
|
|
281
|
+
* @type {string|string[]}
|
|
295
282
|
*/
|
|
296
283
|
convertTypes: ['image/png'],
|
|
297
284
|
/**
|
|
@@ -301,9 +288,9 @@ var DEFAULTS = {
|
|
|
301
288
|
*/
|
|
302
289
|
convertSize: 5000000,
|
|
303
290
|
/**
|
|
304
|
-
* The hook function to execute before
|
|
291
|
+
* The hook function to execute before drawing the image into the canvas for compression.
|
|
305
292
|
* @type {Function}
|
|
306
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
293
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
307
294
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
308
295
|
* @example
|
|
309
296
|
* function (context, canvas) {
|
|
@@ -312,9 +299,9 @@ var DEFAULTS = {
|
|
|
312
299
|
*/
|
|
313
300
|
beforeDraw: null,
|
|
314
301
|
/**
|
|
315
|
-
* The hook function to execute after
|
|
302
|
+
* The hook function to execute after drawing the image into the canvas for compression.
|
|
316
303
|
* @type {Function}
|
|
317
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
304
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
318
305
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
319
306
|
* @example
|
|
320
307
|
* function (context, canvas) {
|
|
@@ -323,7 +310,7 @@ var DEFAULTS = {
|
|
|
323
310
|
*/
|
|
324
311
|
drew: null,
|
|
325
312
|
/**
|
|
326
|
-
* The hook function to execute when
|
|
313
|
+
* The hook function to execute when the image is compressed successfully.
|
|
327
314
|
* @type {Function}
|
|
328
315
|
* @param {File} file - The compressed image File object.
|
|
329
316
|
* @example
|
|
@@ -368,9 +355,9 @@ function toArray(value) {
|
|
|
368
355
|
var REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
369
356
|
|
|
370
357
|
/**
|
|
371
|
-
* Check if the given value is
|
|
358
|
+
* Check if the given value is an image MIME type.
|
|
372
359
|
* @param {*} value - The value to check.
|
|
373
|
-
* @returns {boolean} Returns `true` if the given is
|
|
360
|
+
* @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
|
|
374
361
|
*/
|
|
375
362
|
function isImageType(value) {
|
|
376
363
|
return REGEXP_IMAGE_TYPE.test(value);
|
|
@@ -379,7 +366,7 @@ function isImageType(value) {
|
|
|
379
366
|
/**
|
|
380
367
|
* Convert image type to extension.
|
|
381
368
|
* @param {string} value - The image type to convert.
|
|
382
|
-
* @returns {
|
|
369
|
+
* @returns {string} Returns the image extension.
|
|
383
370
|
*/
|
|
384
371
|
function imageTypeToExtension(value) {
|
|
385
372
|
var extension = isImageType(value) ? value.substr(6) : '';
|
|
@@ -406,12 +393,55 @@ function getStringFromCharCode(dataView, start, length) {
|
|
|
406
393
|
}
|
|
407
394
|
return str;
|
|
408
395
|
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Check if `canvas.getContext('2d').getImageData` is available,
|
|
399
|
+
* Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
|
|
400
|
+
* @link https://github.com/nodeca/pica/blob/master/lib/utils.js
|
|
401
|
+
* @returns {boolean} Returns `true` if it is available, else `false`.
|
|
402
|
+
*/
|
|
403
|
+
function isCanvasAvailable() {
|
|
404
|
+
try {
|
|
405
|
+
var canvas = document.createElement('canvas');
|
|
406
|
+
canvas.width = 2;
|
|
407
|
+
canvas.height = 1;
|
|
408
|
+
var context = canvas.getContext('2d');
|
|
409
|
+
if (!context) return false;
|
|
410
|
+
|
|
411
|
+
// Create 2x1 image data containing RGBA values for two pixels
|
|
412
|
+
var imageData = context.createImageData(2, 1);
|
|
413
|
+
|
|
414
|
+
// First pixel: R=12, G=23, B=34, A=255
|
|
415
|
+
imageData.data[0] = 12;
|
|
416
|
+
imageData.data[1] = 23;
|
|
417
|
+
imageData.data[2] = 34;
|
|
418
|
+
imageData.data[3] = 255;
|
|
419
|
+
|
|
420
|
+
// Second pixel: R=45, G=56, B=67, A=255
|
|
421
|
+
imageData.data[4] = 45;
|
|
422
|
+
imageData.data[5] = 56;
|
|
423
|
+
imageData.data[6] = 67;
|
|
424
|
+
imageData.data[7] = 255;
|
|
425
|
+
context.putImageData(imageData, 0, 0);
|
|
426
|
+
var readBack = context.getImageData(0, 0, 2, 1);
|
|
427
|
+
|
|
428
|
+
// Expected pixel data (matching the written values)
|
|
429
|
+
var expected = [12, 23, 34, 255, 45, 56, 67, 255];
|
|
430
|
+
|
|
431
|
+
// Compare element by element to ensure write and read consistency
|
|
432
|
+
return readBack.data.every(function (value, index) {
|
|
433
|
+
return value === expected[index];
|
|
434
|
+
});
|
|
435
|
+
} catch (error) {
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
409
439
|
var btoa = WINDOW.btoa;
|
|
410
440
|
|
|
411
441
|
/**
|
|
412
442
|
* Transform array buffer to Data URL.
|
|
413
443
|
* @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
|
|
414
|
-
* @param {string} mimeType - The
|
|
444
|
+
* @param {string} mimeType - The MIME type of the Data URL.
|
|
415
445
|
* @returns {string} The result Data URL.
|
|
416
446
|
*/
|
|
417
447
|
function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
@@ -659,11 +689,12 @@ var AnotherCompressor = WINDOW.Compressor;
|
|
|
659
689
|
*/
|
|
660
690
|
var Compressor = /*#__PURE__*/function () {
|
|
661
691
|
/**
|
|
662
|
-
*
|
|
663
|
-
* @param {File|Blob} file - The target image file
|
|
664
|
-
* @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.
|
|
665
695
|
*/
|
|
666
|
-
function Compressor(file
|
|
696
|
+
function Compressor(file) {
|
|
697
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
667
698
|
_classCallCheck(this, Compressor);
|
|
668
699
|
this.file = file;
|
|
669
700
|
this.exif = [];
|
|
@@ -673,13 +704,13 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
673
704
|
this.result = null;
|
|
674
705
|
this.init();
|
|
675
706
|
}
|
|
676
|
-
_createClass(Compressor, [{
|
|
707
|
+
return _createClass(Compressor, [{
|
|
677
708
|
key: "init",
|
|
678
709
|
value: function init() {
|
|
679
710
|
var _this = this;
|
|
680
711
|
var file = this.file,
|
|
681
712
|
options = this.options;
|
|
682
|
-
if (!isBlob(file)) {
|
|
713
|
+
if (!isBlob$1(file)) {
|
|
683
714
|
this.fail(new Error('The first argument must be a File or Blob object.'));
|
|
684
715
|
return;
|
|
685
716
|
}
|
|
@@ -759,10 +790,18 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
759
790
|
var file = this.file,
|
|
760
791
|
image = this.image;
|
|
761
792
|
image.onload = function () {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
793
|
+
if (isCanvasAvailable()) {
|
|
794
|
+
_this2.draw(_objectSpread2(_objectSpread2({}, data), {}, {
|
|
795
|
+
naturalWidth: image.naturalWidth,
|
|
796
|
+
naturalHeight: image.naturalHeight
|
|
797
|
+
}));
|
|
798
|
+
} else {
|
|
799
|
+
_this2.done({
|
|
800
|
+
naturalWidth: image.naturalWidth,
|
|
801
|
+
naturalHeight: image.naturalHeight,
|
|
802
|
+
result: null
|
|
803
|
+
});
|
|
804
|
+
}
|
|
766
805
|
};
|
|
767
806
|
image.onabort = function () {
|
|
768
807
|
_this2.fail(new Error('Aborted to load the image.'));
|
|
@@ -985,14 +1024,25 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
985
1024
|
if (options.strict && !options.retainExif && result.size > file.size && options.mimeType === file.type && !(options.width > naturalWidth || options.height > naturalHeight || options.minWidth > naturalWidth || options.minHeight > naturalHeight || options.maxWidth < naturalWidth || options.maxHeight < naturalHeight)) {
|
|
986
1025
|
result = file;
|
|
987
1026
|
} else {
|
|
988
|
-
var
|
|
989
|
-
result.lastModified = date.getTime();
|
|
990
|
-
result.lastModifiedDate = date;
|
|
991
|
-
result.name = file.name;
|
|
1027
|
+
var fileName = file.name;
|
|
992
1028
|
|
|
993
1029
|
// Convert the extension to match its type
|
|
994
|
-
if (
|
|
995
|
-
|
|
1030
|
+
if (fileName && result.type !== file.type) {
|
|
1031
|
+
fileName = fileName.replace(REGEXP_EXTENSION, imageTypeToExtension(result.type));
|
|
1032
|
+
}
|
|
1033
|
+
try {
|
|
1034
|
+
// Convert the resulting Blob object into a File object for modern browsers.
|
|
1035
|
+
result = new File([result], fileName, {
|
|
1036
|
+
type: result.type
|
|
1037
|
+
});
|
|
1038
|
+
} catch (error) {
|
|
1039
|
+
// Fallback to Blob if the File constructor is not supported.
|
|
1040
|
+
var date = new Date();
|
|
1041
|
+
result.name = fileName;
|
|
1042
|
+
|
|
1043
|
+
// The last modified date is not accurate, but it's better than nothing.
|
|
1044
|
+
result.lastModified = date.getTime();
|
|
1045
|
+
result.lastModifiedDate = date;
|
|
996
1046
|
}
|
|
997
1047
|
}
|
|
998
1048
|
} else {
|
|
@@ -1031,14 +1081,15 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
1031
1081
|
}
|
|
1032
1082
|
|
|
1033
1083
|
/**
|
|
1034
|
-
*
|
|
1035
|
-
* @
|
|
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.
|
|
1036
1088
|
*/
|
|
1037
1089
|
}], [{
|
|
1038
|
-
key: "
|
|
1039
|
-
value: function
|
|
1040
|
-
|
|
1041
|
-
return Compressor;
|
|
1090
|
+
key: "create",
|
|
1091
|
+
value: function create(file, options) {
|
|
1092
|
+
return new Compressor(file, options);
|
|
1042
1093
|
}
|
|
1043
1094
|
|
|
1044
1095
|
/**
|
|
@@ -1050,8 +1101,18 @@ var Compressor = /*#__PURE__*/function () {
|
|
|
1050
1101
|
value: function setDefaults(options) {
|
|
1051
1102
|
_extends(DEFAULTS, options);
|
|
1052
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
|
+
}
|
|
1053
1115
|
}]);
|
|
1054
|
-
return Compressor;
|
|
1055
1116
|
}();
|
|
1056
1117
|
|
|
1057
1118
|
module.exports = Compressor;
|