compressorjs 1.2.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +76 -39
- package/dist/compressor.common.js +172 -111
- package/dist/compressor.esm.js +172 -111
- package/dist/compressor.js +172 -111
- package/dist/compressor.min.js +3 -3
- package/package.json +50 -45
- package/src/defaults.js +9 -9
- package/src/index.js +54 -23
- package/src/utilities.js +51 -7
- package/types/index.d.ts +10 -7
package/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:
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
(function (global, factory) {
|
|
@@ -14,90 +14,75 @@
|
|
|
14
14
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Compressor = factory());
|
|
15
15
|
})(this, (function () { 'use strict';
|
|
16
16
|
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
if (Object.getOwnPropertySymbols) {
|
|
20
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
21
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
22
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
23
|
-
})), keys.push.apply(keys, symbols);
|
|
24
|
-
}
|
|
25
|
-
return keys;
|
|
17
|
+
function _classCallCheck(a, n) {
|
|
18
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
26
19
|
}
|
|
27
|
-
function
|
|
28
|
-
for (var
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
_defineProperty(target, key, source[key]);
|
|
32
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
33
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
34
|
-
});
|
|
20
|
+
function _defineProperties(e, r) {
|
|
21
|
+
for (var t = 0; t < r.length; t++) {
|
|
22
|
+
var o = r[t];
|
|
23
|
+
o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
35
24
|
}
|
|
36
|
-
return target;
|
|
37
25
|
}
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
26
|
+
function _createClass(e, r, t) {
|
|
27
|
+
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
|
|
28
|
+
writable: !1
|
|
29
|
+
}), e;
|
|
42
30
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
31
|
+
function _defineProperty(e, r, t) {
|
|
32
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
33
|
+
value: t,
|
|
34
|
+
enumerable: !0,
|
|
35
|
+
configurable: !0,
|
|
36
|
+
writable: !0
|
|
37
|
+
}) : e[r] = t, e;
|
|
51
38
|
}
|
|
52
|
-
function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
39
|
+
function _extends() {
|
|
40
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
41
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
42
|
+
var t = arguments[e];
|
|
43
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
44
|
+
}
|
|
45
|
+
return n;
|
|
46
|
+
}, _extends.apply(null, arguments);
|
|
59
47
|
}
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
Object.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
writable: true
|
|
68
|
-
});
|
|
69
|
-
} else {
|
|
70
|
-
obj[key] = value;
|
|
48
|
+
function ownKeys(e, r) {
|
|
49
|
+
var t = Object.keys(e);
|
|
50
|
+
if (Object.getOwnPropertySymbols) {
|
|
51
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
52
|
+
r && (o = o.filter(function (r) {
|
|
53
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
54
|
+
})), t.push.apply(t, o);
|
|
71
55
|
}
|
|
72
|
-
return
|
|
56
|
+
return t;
|
|
73
57
|
}
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return target;
|
|
85
|
-
};
|
|
86
|
-
return _extends.apply(this, arguments);
|
|
58
|
+
function _objectSpread2(e) {
|
|
59
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
60
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
61
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
62
|
+
_defineProperty(e, r, t[r]);
|
|
63
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
64
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return e;
|
|
87
68
|
}
|
|
88
|
-
function _toPrimitive(
|
|
89
|
-
if (
|
|
90
|
-
var
|
|
91
|
-
if (
|
|
92
|
-
var
|
|
93
|
-
if (
|
|
69
|
+
function _toPrimitive(t, r) {
|
|
70
|
+
if ("object" != typeof t || !t) return t;
|
|
71
|
+
var e = t[Symbol.toPrimitive];
|
|
72
|
+
if (void 0 !== e) {
|
|
73
|
+
var i = e.call(t, r || "default");
|
|
74
|
+
if ("object" != typeof i) return i;
|
|
94
75
|
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
95
76
|
}
|
|
96
|
-
return (
|
|
77
|
+
return ("string" === r ? String : Number)(t);
|
|
97
78
|
}
|
|
98
|
-
function _toPropertyKey(
|
|
99
|
-
var
|
|
100
|
-
return
|
|
79
|
+
function _toPropertyKey(t) {
|
|
80
|
+
var i = _toPrimitive(t, "string");
|
|
81
|
+
return "symbol" == typeof i ? i : i + "";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getDefaultExportFromCjs (x) {
|
|
85
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
101
86
|
}
|
|
102
87
|
|
|
103
88
|
var canvasToBlob = {exports: {}};
|
|
@@ -213,7 +198,8 @@
|
|
|
213
198
|
}
|
|
214
199
|
})(window);
|
|
215
200
|
})(canvasToBlob);
|
|
216
|
-
var
|
|
201
|
+
var canvasToBlobExports = canvasToBlob.exports;
|
|
202
|
+
var toBlob = /*@__PURE__*/getDefaultExportFromCjs(canvasToBlobExports);
|
|
217
203
|
|
|
218
204
|
var isBlob = function isBlob(value) {
|
|
219
205
|
if (typeof Blob === 'undefined') {
|
|
@@ -221,22 +207,23 @@
|
|
|
221
207
|
}
|
|
222
208
|
return value instanceof Blob || Object.prototype.toString.call(value) === '[object Blob]';
|
|
223
209
|
};
|
|
210
|
+
var isBlob$1 = /*@__PURE__*/getDefaultExportFromCjs(isBlob);
|
|
224
211
|
|
|
225
212
|
var DEFAULTS = {
|
|
226
213
|
/**
|
|
227
|
-
* Indicates
|
|
214
|
+
* Indicates whether to output the original image instead of the compressed one
|
|
228
215
|
* when the size of the compressed image is greater than the original one's
|
|
229
216
|
* @type {boolean}
|
|
230
217
|
*/
|
|
231
218
|
strict: true,
|
|
232
219
|
/**
|
|
233
|
-
* Indicates
|
|
220
|
+
* Indicates whether to read the image's Exif Orientation information,
|
|
234
221
|
* and then rotate or flip the image automatically.
|
|
235
222
|
* @type {boolean}
|
|
236
223
|
*/
|
|
237
224
|
checkOrientation: true,
|
|
238
225
|
/**
|
|
239
|
-
* Indicates
|
|
226
|
+
* Indicates whether to retain the image's Exif information after compression.
|
|
240
227
|
* @type {boolean}
|
|
241
228
|
*/
|
|
242
229
|
retainExif: false,
|
|
@@ -295,7 +282,7 @@
|
|
|
295
282
|
/**
|
|
296
283
|
* Files whose file type is included in this list,
|
|
297
284
|
* and whose file size exceeds the `convertSize` value will be converted to JPEGs.
|
|
298
|
-
* @type {string
|
|
285
|
+
* @type {string|string[]}
|
|
299
286
|
*/
|
|
300
287
|
convertTypes: ['image/png'],
|
|
301
288
|
/**
|
|
@@ -305,9 +292,9 @@
|
|
|
305
292
|
*/
|
|
306
293
|
convertSize: 5000000,
|
|
307
294
|
/**
|
|
308
|
-
* The hook function to execute before
|
|
295
|
+
* The hook function to execute before drawing the image into the canvas for compression.
|
|
309
296
|
* @type {Function}
|
|
310
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
297
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
311
298
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
312
299
|
* @example
|
|
313
300
|
* function (context, canvas) {
|
|
@@ -316,9 +303,9 @@
|
|
|
316
303
|
*/
|
|
317
304
|
beforeDraw: null,
|
|
318
305
|
/**
|
|
319
|
-
* The hook function to execute after
|
|
306
|
+
* The hook function to execute after drawing the image into the canvas for compression.
|
|
320
307
|
* @type {Function}
|
|
321
|
-
* @param {CanvasRenderingContext2D} context - The
|
|
308
|
+
* @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
|
|
322
309
|
* @param {HTMLCanvasElement} canvas - The canvas for compression.
|
|
323
310
|
* @example
|
|
324
311
|
* function (context, canvas) {
|
|
@@ -327,7 +314,7 @@
|
|
|
327
314
|
*/
|
|
328
315
|
drew: null,
|
|
329
316
|
/**
|
|
330
|
-
* The hook function to execute when
|
|
317
|
+
* The hook function to execute when the image is compressed successfully.
|
|
331
318
|
* @type {Function}
|
|
332
319
|
* @param {File} file - The compressed image File object.
|
|
333
320
|
* @example
|
|
@@ -372,9 +359,9 @@
|
|
|
372
359
|
var REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
373
360
|
|
|
374
361
|
/**
|
|
375
|
-
* Check if the given value is
|
|
362
|
+
* Check if the given value is an image MIME type.
|
|
376
363
|
* @param {*} value - The value to check.
|
|
377
|
-
* @returns {boolean} Returns `true` if the given is
|
|
364
|
+
* @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
|
|
378
365
|
*/
|
|
379
366
|
function isImageType(value) {
|
|
380
367
|
return REGEXP_IMAGE_TYPE.test(value);
|
|
@@ -383,7 +370,7 @@
|
|
|
383
370
|
/**
|
|
384
371
|
* Convert image type to extension.
|
|
385
372
|
* @param {string} value - The image type to convert.
|
|
386
|
-
* @returns {
|
|
373
|
+
* @returns {string} Returns the image extension.
|
|
387
374
|
*/
|
|
388
375
|
function imageTypeToExtension(value) {
|
|
389
376
|
var extension = isImageType(value) ? value.substr(6) : '';
|
|
@@ -410,12 +397,55 @@
|
|
|
410
397
|
}
|
|
411
398
|
return str;
|
|
412
399
|
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Check if `canvas.getContext('2d').getImageData` is available,
|
|
403
|
+
* Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
|
|
404
|
+
* @link https://github.com/nodeca/pica/blob/master/lib/utils.js
|
|
405
|
+
* @returns {boolean} Returns `true` if it is available, else `false`.
|
|
406
|
+
*/
|
|
407
|
+
function isCanvasAvailable() {
|
|
408
|
+
try {
|
|
409
|
+
var canvas = document.createElement('canvas');
|
|
410
|
+
canvas.width = 2;
|
|
411
|
+
canvas.height = 1;
|
|
412
|
+
var context = canvas.getContext('2d');
|
|
413
|
+
if (!context) return false;
|
|
414
|
+
|
|
415
|
+
// Create 2x1 image data containing RGBA values for two pixels
|
|
416
|
+
var imageData = context.createImageData(2, 1);
|
|
417
|
+
|
|
418
|
+
// First pixel: R=12, G=23, B=34, A=255
|
|
419
|
+
imageData.data[0] = 12;
|
|
420
|
+
imageData.data[1] = 23;
|
|
421
|
+
imageData.data[2] = 34;
|
|
422
|
+
imageData.data[3] = 255;
|
|
423
|
+
|
|
424
|
+
// Second pixel: R=45, G=56, B=67, A=255
|
|
425
|
+
imageData.data[4] = 45;
|
|
426
|
+
imageData.data[5] = 56;
|
|
427
|
+
imageData.data[6] = 67;
|
|
428
|
+
imageData.data[7] = 255;
|
|
429
|
+
context.putImageData(imageData, 0, 0);
|
|
430
|
+
var readBack = context.getImageData(0, 0, 2, 1);
|
|
431
|
+
|
|
432
|
+
// Expected pixel data (matching the written values)
|
|
433
|
+
var expected = [12, 23, 34, 255, 45, 56, 67, 255];
|
|
434
|
+
|
|
435
|
+
// Compare element by element to ensure write and read consistency
|
|
436
|
+
return readBack.data.every(function (value, index) {
|
|
437
|
+
return value === expected[index];
|
|
438
|
+
});
|
|
439
|
+
} catch (error) {
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
413
443
|
var btoa = WINDOW.btoa;
|
|
414
444
|
|
|
415
445
|
/**
|
|
416
446
|
* Transform array buffer to Data URL.
|
|
417
447
|
* @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
|
|
418
|
-
* @param {string} mimeType - The
|
|
448
|
+
* @param {string} mimeType - The MIME type of the Data URL.
|
|
419
449
|
* @returns {string} The result Data URL.
|
|
420
450
|
*/
|
|
421
451
|
function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
@@ -663,11 +693,12 @@
|
|
|
663
693
|
*/
|
|
664
694
|
var Compressor = /*#__PURE__*/function () {
|
|
665
695
|
/**
|
|
666
|
-
*
|
|
667
|
-
* @param {File|Blob} file - The target image file
|
|
668
|
-
* @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.
|
|
669
699
|
*/
|
|
670
|
-
function Compressor(file
|
|
700
|
+
function Compressor(file) {
|
|
701
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
671
702
|
_classCallCheck(this, Compressor);
|
|
672
703
|
this.file = file;
|
|
673
704
|
this.exif = [];
|
|
@@ -677,13 +708,13 @@
|
|
|
677
708
|
this.result = null;
|
|
678
709
|
this.init();
|
|
679
710
|
}
|
|
680
|
-
_createClass(Compressor, [{
|
|
711
|
+
return _createClass(Compressor, [{
|
|
681
712
|
key: "init",
|
|
682
713
|
value: function init() {
|
|
683
714
|
var _this = this;
|
|
684
715
|
var file = this.file,
|
|
685
716
|
options = this.options;
|
|
686
|
-
if (!isBlob(file)) {
|
|
717
|
+
if (!isBlob$1(file)) {
|
|
687
718
|
this.fail(new Error('The first argument must be a File or Blob object.'));
|
|
688
719
|
return;
|
|
689
720
|
}
|
|
@@ -763,10 +794,18 @@
|
|
|
763
794
|
var file = this.file,
|
|
764
795
|
image = this.image;
|
|
765
796
|
image.onload = function () {
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
797
|
+
if (isCanvasAvailable()) {
|
|
798
|
+
_this2.draw(_objectSpread2(_objectSpread2({}, data), {}, {
|
|
799
|
+
naturalWidth: image.naturalWidth,
|
|
800
|
+
naturalHeight: image.naturalHeight
|
|
801
|
+
}));
|
|
802
|
+
} else {
|
|
803
|
+
_this2.done({
|
|
804
|
+
naturalWidth: image.naturalWidth,
|
|
805
|
+
naturalHeight: image.naturalHeight,
|
|
806
|
+
result: null
|
|
807
|
+
});
|
|
808
|
+
}
|
|
770
809
|
};
|
|
771
810
|
image.onabort = function () {
|
|
772
811
|
_this2.fail(new Error('Aborted to load the image.'));
|
|
@@ -989,14 +1028,25 @@
|
|
|
989
1028
|
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)) {
|
|
990
1029
|
result = file;
|
|
991
1030
|
} else {
|
|
992
|
-
var
|
|
993
|
-
result.lastModified = date.getTime();
|
|
994
|
-
result.lastModifiedDate = date;
|
|
995
|
-
result.name = file.name;
|
|
1031
|
+
var fileName = file.name;
|
|
996
1032
|
|
|
997
1033
|
// Convert the extension to match its type
|
|
998
|
-
if (
|
|
999
|
-
|
|
1034
|
+
if (fileName && result.type !== file.type) {
|
|
1035
|
+
fileName = fileName.replace(REGEXP_EXTENSION, imageTypeToExtension(result.type));
|
|
1036
|
+
}
|
|
1037
|
+
try {
|
|
1038
|
+
// Convert the resulting Blob object into a File object for modern browsers.
|
|
1039
|
+
result = new File([result], fileName, {
|
|
1040
|
+
type: result.type
|
|
1041
|
+
});
|
|
1042
|
+
} catch (error) {
|
|
1043
|
+
// Fallback to Blob if the File constructor is not supported.
|
|
1044
|
+
var date = new Date();
|
|
1045
|
+
result.name = fileName;
|
|
1046
|
+
|
|
1047
|
+
// The last modified date is not accurate, but it's better than nothing.
|
|
1048
|
+
result.lastModified = date.getTime();
|
|
1049
|
+
result.lastModifiedDate = date;
|
|
1000
1050
|
}
|
|
1001
1051
|
}
|
|
1002
1052
|
} else {
|
|
@@ -1035,14 +1085,15 @@
|
|
|
1035
1085
|
}
|
|
1036
1086
|
|
|
1037
1087
|
/**
|
|
1038
|
-
*
|
|
1039
|
-
* @
|
|
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.
|
|
1040
1092
|
*/
|
|
1041
1093
|
}], [{
|
|
1042
|
-
key: "
|
|
1043
|
-
value: function
|
|
1044
|
-
|
|
1045
|
-
return Compressor;
|
|
1094
|
+
key: "create",
|
|
1095
|
+
value: function create(file, options) {
|
|
1096
|
+
return new Compressor(file, options);
|
|
1046
1097
|
}
|
|
1047
1098
|
|
|
1048
1099
|
/**
|
|
@@ -1054,8 +1105,18 @@
|
|
|
1054
1105
|
value: function setDefaults(options) {
|
|
1055
1106
|
_extends(DEFAULTS, options);
|
|
1056
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
|
+
}
|
|
1057
1119
|
}]);
|
|
1058
|
-
return Compressor;
|
|
1059
1120
|
}();
|
|
1060
1121
|
|
|
1061
1122
|
return Compressor;
|
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:
|
|
8
|
+
* Date: 2026-10-01T04:45:36.902Z
|
|
9
9
|
*/
|
|
10
|
-
!function(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(){"use strict";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 n=null!=arguments[e]?arguments[e]:{};e%2?t(Object(n),!0).forEach(function(e){var t,r;t=i,r=n[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(n)):t(Object(n)).forEach(function(e){Object.defineProperty(i,e,Object.getOwnPropertyDescriptor(n,e))})}return i}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)Object.prototype.hasOwnProperty.call(i,r)&&(e[r]=i[r])}return e}).apply(this,arguments)}function o(e){e=function(e,t){if("object"!=typeof e||null===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:String(e)}function O(e){return 0<e&&e<1/0}var e,r,l,c,h,u,f,d,i={exports:{}},j=(e=i,"undefined"!=typeof window&&(l=(r=window).HTMLCanvasElement&&r.HTMLCanvasElement.prototype,c=r.Blob&&function(){try{return Boolean(new Blob)}catch(e){return!1}}(),h=c&&r.Uint8Array&&function(){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,n,a,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"),a=!!o[4],e=e.slice(o[0].length),r=(a?atob:decodeURIComponent)(e),o=new ArrayBuffer(r.length),i=new Uint8Array(o),n=0;n<r.length;n+=1)i[n]=r.charCodeAt(n);return c?new Blob([h?i:o],{type:t}):((a=new u).append(o),a.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},b="undefined"!=typeof window&&void 0!==window.document?window:{},p=Array.prototype.slice;function x(e){return Array.from?Array.from(e):p.call(e)}var g=/^image\/.+$/;function T(e){return g.test(e)}var y=String.fromCharCode;var w=b.btoa;function A(e,t){for(var r=[],i=new Uint8Array(e);0<i.length;)r.push(y.apply(null,x(i.subarray(0,8192)))),i=i.subarray(8192);return"data:".concat(t,";base64,").concat(w(r.join("")))}function v(e){var t,r,i,n,a,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&&(n=r+10,"Exif"===function(e,t,r){var i,n="";for(r+=t,i=t;i<r;i+=1)n+=y(e.getUint8(i));return n}(s,r+4,4))&&((l=18761===(a=s.getUint16(n)))||19789===a)&&42===s.getUint16(n+2,l)&&8<=(o=s.getUint32(n+4,l))?n+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,n=e.height,e=e.width,t=1<arguments.length&&void 0!==t?t:"none",a=O(e),o=O(n);return a&&o?(r=n*i,("contain"===t||"none"===t)&&e<r||"cover"===t&&r<e?n=e/i:e=n*i):a?n=e/i:o&&(e=n*i),{width:e,height:n}}var U=b.ArrayBuffer,k=b.FileReader,M=b.URL||b.webkitURL,D=/\.\w+$/,L=b.Compressor;return function(){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()}var e,t,i;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,n,a,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)?M&&k?(U||(t.checkOrientation=!1,t.retainExif=!1),n=(e="image/jpeg"===i)&&t.checkOrientation,a=e&&t.retainExif,!M||n||a?(e=new k,(this.reader=e).onload=function(e){var e=e.target.result,t={},r=1;n&&1<(r=v(e))&&s(t,function(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)),a&&(o.exif=function(e){for(var t=x(new Uint8Array(e)),r=t.length,i=[],n=0;n+3<r;){var a=t[n],o=t[n+1];if(255===a&&218===o)break;255===a&&216===o?n+=2:(a=n+(256*t[n+2]+t[n+3])+2,o=t.slice(n,a),i.push(o),n=a)}return i.reduce(function(e,t){return 255===t[0]&&225===t[1]?e.concat(t):e},[])}(e)),t.url=n||a?!M||1<r?A(e,i):M.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},n||a?e.readAsArrayBuffer(l):e.readAsDataURL(l)):this.load({url:M.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(){t.draw(n(n({},e),{},{naturalWidth:i.naturalWidth,naturalHeight:i.naturalHeight}))},i.onabort=function(){t.fail(new Error("Aborted to load the image."))},i.onerror=function(){t.fail(new Error("Failed to load the image."))},b.navigator&&/(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(b.navigator.userAgent)&&(i.crossOrigin="anonymous"),i.alt=r.name,i.src=e.url}},{key:"draw",value:function(e){var n=this,a=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)&&O(s.width)&&O(s.height),d=Math.max(s.maxWidth,0)||1/0,m=Math.max(s.maxHeight,0)||1/0,b=Math.max(s.minWidth,0)||0,p=Math.max(s.minHeight,0)||0,g=a/o,y=s.width,w=s.height,v=(u&&(d=(v=[m,d])[0],m=v[1],b=(v=[p,b])[0],p=v[1],y=(v=[w,y])[0],w=v[1]),E({aspectRatio:g=f?y/w:g,width:d,height:m},"contain")),v=(d=v.width,m=v.height,E({aspectRatio:g,width:b,height:p},"cover")),v=(b=v.width,p=v.height,w=f?(y=(v=E({aspectRatio:g,width:y,height:w},s.resize)).width,v.height):(y=void 0===(B=(v=E({aspectRatio:g,width:y,height:w})).width)?a:B,void 0===(B=v.height)?o:B),-(y=Math.floor(R(Math.min(Math.max(y,b),d))))/2),B=-(w=Math.floor(R(Math.min(Math.max(w,p),m))))/2,b=y,d=w,p=[],m=(f&&(0,m=(g=E({aspectRatio:g,width:m=a,height:f=o},{contain:"cover",cover:"contain"}[s.resize])).width,f=g.height,p.push((a-m)/2,(o-f)/2,m,f)),p.push(v,B,b,d),u&&(y=(g=[w,y])[0],w=g[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;n.aborted||(i=function(e){return n.done({naturalWidth:a,naturalHeight:o,result:e})},e&&U&&s.retainExif&&n.exif&&0<n.exif.length?(t=function(e){return i(j(A((e=e,t=n.exif,255!==(r=x(new Uint8Array(e)))[2]||224!==r[3]?e:(e=256*r[4]+r[5],t=[255,216].concat(t,r.slice(4+e)),new Uint8Array(t))),s.mimeType)));var t,r},e.arrayBuffer?e.arrayBuffer().then(t).catch(function(){n.fail(new Error("Failed to read the compressed image with Blob.arrayBuffer()."))}):(r=new k,(n.reader=r).onload=function(e){e=e.target;t(e.result)},r.onabort=function(){n.fail(new Error("Aborted to read the compressed image with FileReader."))},r.onerror=function(){n.fail(new Error("Failed to read the compressed image with FileReader."))},r.onloadend=function(){n.reader=null},r.readAsArrayBuffer(e))):i(e))},c.toBlob?c.toBlob(f,s.mimeType,s.quality):f(j(c.toDataURL(s.mimeType,s.quality))))}},{key:"done",value:function(e){var t=e.naturalWidth,r=e.naturalHeight,e=e.result,i=this.file,n=this.image,a=this.options;M&&0===n.src.indexOf("blob:")&&M.revokeObjectURL(n.src),!e||a.strict&&!a.retainExif&&e.size>i.size&&a.mimeType===i.type&&!(a.width>t||a.height>r||a.minWidth>t||a.minHeight>r||a.maxWidth<t||a.maxHeight<r)?e=i:(n=new Date,e.lastModified=n.getTime(),e.lastModifiedDate=n,e.name=i.name,e.name&&e.type!==i.type&&(e.name=e.name.replace(D,(t=T(t=e.type)?t.substr(6):"",".".concat(t="jpeg"===t?"jpg":t))))),this.result=e,a.success&&a.success.call(this,e)}},{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}),r}()});
|
|
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,23 +1,33 @@
|
|
|
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",
|
|
7
7
|
"browser": "dist/compressor.js",
|
|
8
8
|
"types": "types/index.d.ts",
|
|
9
|
-
"repository": "fengyuanchen/compressorjs",
|
|
10
|
-
"homepage": "https://fengyuanchen.github.io/compressorjs",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"author": {
|
|
13
|
-
"name": "Chen Fengyuan",
|
|
14
|
-
"url": "https://chenfengyuan.com/"
|
|
15
|
-
},
|
|
16
9
|
"files": [
|
|
17
10
|
"src",
|
|
18
11
|
"dist",
|
|
19
12
|
"types"
|
|
20
13
|
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "rollup -c",
|
|
16
|
+
"clean": "del-cli dist",
|
|
17
|
+
"compress": "uglifyjs dist/compressor.js -o dist/compressor.min.js -c -m --comments /^!/",
|
|
18
|
+
"dev": "rollup -c -m -w",
|
|
19
|
+
"lint": "npm run lint:js && npm run lint:css",
|
|
20
|
+
"lint:css": "stylelint docs/**/*.css --fix",
|
|
21
|
+
"lint:js": "eslint src test *.js --fix",
|
|
22
|
+
"prepare": "simple-git-hooks",
|
|
23
|
+
"release": "npm run clean && npm run lint && npm run build && npm run compress && npm test",
|
|
24
|
+
"start": "npm run dev",
|
|
25
|
+
"test": "cross-env NODE_ENV=test karma start test/karma.conf.js"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/fengyuanchen/compressorjs.git"
|
|
30
|
+
},
|
|
21
31
|
"keywords": [
|
|
22
32
|
"image",
|
|
23
33
|
"compress",
|
|
@@ -29,59 +39,54 @@
|
|
|
29
39
|
"front-end",
|
|
30
40
|
"web"
|
|
31
41
|
],
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
"lint:js": "eslint src test *.js --fix",
|
|
40
|
-
"prepare": "husky install",
|
|
41
|
-
"release": "npm run clean && npm run lint && npm run build && npm run compress && npm test",
|
|
42
|
-
"start": "npm run dev",
|
|
43
|
-
"test": "cross-env NODE_ENV=test karma start test/karma.conf.js",
|
|
44
|
-
"test:coverage": "cat coverage/lcov.info | codecov"
|
|
42
|
+
"author": {
|
|
43
|
+
"name": "Chen Fengyuan",
|
|
44
|
+
"url": "https://chenfengyuan.com/"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/fengyuanchen/compressorjs/issues"
|
|
45
49
|
},
|
|
50
|
+
"homepage": "https://fengyuanchen.github.io/compressorjs",
|
|
46
51
|
"dependencies": {
|
|
47
52
|
"blueimp-canvas-to-blob": "^3.29.0",
|
|
48
53
|
"is-blob": "^2.1.0"
|
|
49
54
|
},
|
|
50
55
|
"devDependencies": {
|
|
51
|
-
"@babel/core": "^7.
|
|
52
|
-
"@babel/plugin-transform-object-assign": "^7.
|
|
53
|
-
"@babel/preset-env": "^7.
|
|
54
|
-
"@commitlint/cli": "^17.
|
|
55
|
-
"@commitlint/config-conventional": "^17.
|
|
56
|
-
"@rollup/plugin-babel": "^
|
|
57
|
-
"@rollup/plugin-commonjs": "^
|
|
58
|
-
"@rollup/plugin-node-resolve": "^
|
|
59
|
-
"@rollup/plugin-replace": "^
|
|
60
|
-
"babel-plugin-istanbul": "^
|
|
61
|
-
"chai": "^4.
|
|
56
|
+
"@babel/core": "^7.29.7",
|
|
57
|
+
"@babel/plugin-transform-object-assign": "^7.29.7",
|
|
58
|
+
"@babel/preset-env": "^7.29.7",
|
|
59
|
+
"@commitlint/cli": "^17.8.1",
|
|
60
|
+
"@commitlint/config-conventional": "^17.8.1",
|
|
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
|
+
"chai": "^4.5.0",
|
|
62
67
|
"change-case": "^4.1.2",
|
|
63
68
|
"codecov": "^3.8.3",
|
|
64
69
|
"create-banner": "^2.0.0",
|
|
65
70
|
"cross-env": "^7.0.3",
|
|
66
|
-
"del-cli": "^5.
|
|
67
|
-
"eslint": "^8.
|
|
71
|
+
"del-cli": "^5.1.0",
|
|
72
|
+
"eslint": "^8.57.1",
|
|
68
73
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
69
|
-
"eslint-plugin-import": "^2.
|
|
70
|
-
"
|
|
71
|
-
"karma": "^6.4.1",
|
|
74
|
+
"eslint-plugin-import": "^2.32.0",
|
|
75
|
+
"karma": "^6.4.4",
|
|
72
76
|
"karma-chai": "^0.1.0",
|
|
73
|
-
"karma-chrome-launcher": "^3.
|
|
77
|
+
"karma-chrome-launcher": "^3.2.0",
|
|
74
78
|
"karma-coverage-istanbul-reporter": "^3.0.3",
|
|
75
79
|
"karma-mocha": "^2.0.1",
|
|
76
80
|
"karma-mocha-reporter": "^2.2.5",
|
|
77
81
|
"karma-rollup-preprocessor": "^7.0.8",
|
|
78
|
-
"lint-staged": "^13.
|
|
79
|
-
"mocha": "^10.2
|
|
80
|
-
"puppeteer": "^19.
|
|
81
|
-
"rollup": "^
|
|
82
|
-
"
|
|
82
|
+
"lint-staged": "^13.3.0",
|
|
83
|
+
"mocha": "^10.8.2",
|
|
84
|
+
"puppeteer": "^19.11.1",
|
|
85
|
+
"rollup": "^3.30.0",
|
|
86
|
+
"simple-git-hooks": "^2.13.1",
|
|
87
|
+
"stylelint": "^15.11.0",
|
|
83
88
|
"stylelint-config-standard": "^30.0.1",
|
|
84
|
-
"stylelint-order": "^6.0.
|
|
85
|
-
"uglify-js": "^3.
|
|
89
|
+
"stylelint-order": "^6.0.4",
|
|
90
|
+
"uglify-js": "^3.19.3"
|
|
86
91
|
}
|
|
87
92
|
}
|