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