compressorjs 1.2.0 → 1.2.1
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/dist/compressor.common.js +503 -434
- package/dist/compressor.esm.js +503 -434
- package/dist/compressor.js +503 -434
- package/dist/compressor.min.js +3 -3
- package/package.json +2 -2
package/dist/compressor.esm.js
CHANGED
|
@@ -1,13 +1,70 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Compressor.js v1.2.
|
|
2
|
+
* Compressor.js v1.2.1
|
|
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-
|
|
8
|
+
* Date: 2023-02-28T14:09:41.732Z
|
|
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;
|
|
20
|
+
}
|
|
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
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
}
|
|
32
|
+
function _classCallCheck(instance, Constructor) {
|
|
33
|
+
if (!(instance instanceof Constructor)) {
|
|
34
|
+
throw new TypeError("Cannot call a class as a function");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
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
|
+
}
|
|
45
|
+
}
|
|
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;
|
|
53
|
+
}
|
|
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;
|
|
65
|
+
}
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
11
68
|
function _extends() {
|
|
12
69
|
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
13
70
|
for (var i = 1; i < arguments.length; i++) {
|
|
@@ -22,6 +79,20 @@ function _extends() {
|
|
|
22
79
|
};
|
|
23
80
|
return _extends.apply(this, arguments);
|
|
24
81
|
}
|
|
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;
|
|
88
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
89
|
+
}
|
|
90
|
+
return (hint === "string" ? String : Number)(input);
|
|
91
|
+
}
|
|
92
|
+
function _toPropertyKey(arg) {
|
|
93
|
+
var key = _toPrimitive(arg, "string");
|
|
94
|
+
return typeof key === "symbol" ? key : String(key);
|
|
95
|
+
}
|
|
25
96
|
|
|
26
97
|
var canvasToBlob = {exports: {}};
|
|
27
98
|
|
|
@@ -138,7 +209,7 @@ var canvasToBlob = {exports: {}};
|
|
|
138
209
|
})(canvasToBlob);
|
|
139
210
|
var toBlob = canvasToBlob.exports;
|
|
140
211
|
|
|
141
|
-
var isBlob = value
|
|
212
|
+
var isBlob = function isBlob(value) {
|
|
142
213
|
if (typeof Blob === 'undefined') {
|
|
143
214
|
return false;
|
|
144
215
|
}
|
|
@@ -271,18 +342,18 @@ var DEFAULTS = {
|
|
|
271
342
|
error: null
|
|
272
343
|
};
|
|
273
344
|
|
|
274
|
-
|
|
275
|
-
|
|
345
|
+
var IS_BROWSER = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
346
|
+
var WINDOW = IS_BROWSER ? window : {};
|
|
276
347
|
|
|
277
348
|
/**
|
|
278
349
|
* Check if the given value is a positive number.
|
|
279
350
|
* @param {*} value - The value to check.
|
|
280
351
|
* @returns {boolean} Returns `true` if the given value is a positive number, else `false`.
|
|
281
352
|
*/
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
353
|
+
var isPositiveNumber = function isPositiveNumber(value) {
|
|
354
|
+
return value > 0 && value < Infinity;
|
|
355
|
+
};
|
|
356
|
+
var slice = Array.prototype.slice;
|
|
286
357
|
|
|
287
358
|
/**
|
|
288
359
|
* Convert array-like or iterable object to an array.
|
|
@@ -292,7 +363,7 @@ const {
|
|
|
292
363
|
function toArray(value) {
|
|
293
364
|
return Array.from ? Array.from(value) : slice.call(value);
|
|
294
365
|
}
|
|
295
|
-
|
|
366
|
+
var REGEXP_IMAGE_TYPE = /^image\/.+$/;
|
|
296
367
|
|
|
297
368
|
/**
|
|
298
369
|
* Check if the given value is a mime type of image.
|
|
@@ -309,15 +380,13 @@ function isImageType(value) {
|
|
|
309
380
|
* @returns {boolean} Returns the image extension.
|
|
310
381
|
*/
|
|
311
382
|
function imageTypeToExtension(value) {
|
|
312
|
-
|
|
383
|
+
var extension = isImageType(value) ? value.substr(6) : '';
|
|
313
384
|
if (extension === 'jpeg') {
|
|
314
385
|
extension = 'jpg';
|
|
315
386
|
}
|
|
316
|
-
return
|
|
387
|
+
return ".".concat(extension);
|
|
317
388
|
}
|
|
318
|
-
|
|
319
|
-
fromCharCode
|
|
320
|
-
} = String;
|
|
389
|
+
var fromCharCode = String.fromCharCode;
|
|
321
390
|
|
|
322
391
|
/**
|
|
323
392
|
* Get string from char code in data view.
|
|
@@ -327,17 +396,15 @@ const {
|
|
|
327
396
|
* @returns {string} The read result.
|
|
328
397
|
*/
|
|
329
398
|
function getStringFromCharCode(dataView, start, length) {
|
|
330
|
-
|
|
331
|
-
|
|
399
|
+
var str = '';
|
|
400
|
+
var i;
|
|
332
401
|
length += start;
|
|
333
402
|
for (i = start; i < length; i += 1) {
|
|
334
403
|
str += fromCharCode(dataView.getUint8(i));
|
|
335
404
|
}
|
|
336
405
|
return str;
|
|
337
406
|
}
|
|
338
|
-
|
|
339
|
-
btoa
|
|
340
|
-
} = WINDOW;
|
|
407
|
+
var btoa = WINDOW.btoa;
|
|
341
408
|
|
|
342
409
|
/**
|
|
343
410
|
* Transform array buffer to Data URL.
|
|
@@ -346,16 +413,16 @@ const {
|
|
|
346
413
|
* @returns {string} The result Data URL.
|
|
347
414
|
*/
|
|
348
415
|
function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
416
|
+
var chunks = [];
|
|
417
|
+
var chunkSize = 8192;
|
|
418
|
+
var uint8 = new Uint8Array(arrayBuffer);
|
|
352
419
|
while (uint8.length > 0) {
|
|
353
420
|
// XXX: Babel's `toConsumableArray` helper will throw error in IE or Safari 9
|
|
354
421
|
// eslint-disable-next-line prefer-spread
|
|
355
422
|
chunks.push(fromCharCode.apply(null, toArray(uint8.subarray(0, chunkSize))));
|
|
356
423
|
uint8 = uint8.subarray(chunkSize);
|
|
357
424
|
}
|
|
358
|
-
return
|
|
425
|
+
return "data:".concat(mimeType, ";base64,").concat(btoa(chunks.join('')));
|
|
359
426
|
}
|
|
360
427
|
|
|
361
428
|
/**
|
|
@@ -364,19 +431,19 @@ function arrayBufferToDataURL(arrayBuffer, mimeType) {
|
|
|
364
431
|
* @returns {number} The read orientation value.
|
|
365
432
|
*/
|
|
366
433
|
function resetAndGetOrientation(arrayBuffer) {
|
|
367
|
-
|
|
368
|
-
|
|
434
|
+
var dataView = new DataView(arrayBuffer);
|
|
435
|
+
var orientation;
|
|
369
436
|
|
|
370
437
|
// Ignores range error when the image does not have correct Exif information
|
|
371
438
|
try {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
439
|
+
var littleEndian;
|
|
440
|
+
var app1Start;
|
|
441
|
+
var ifdStart;
|
|
375
442
|
|
|
376
443
|
// Only handle JPEG image (start by 0xFFD8)
|
|
377
444
|
if (dataView.getUint8(0) === 0xFF && dataView.getUint8(1) === 0xD8) {
|
|
378
|
-
|
|
379
|
-
|
|
445
|
+
var length = dataView.byteLength;
|
|
446
|
+
var offset = 2;
|
|
380
447
|
while (offset + 1 < length) {
|
|
381
448
|
if (dataView.getUint8(offset) === 0xFF && dataView.getUint8(offset + 1) === 0xE1) {
|
|
382
449
|
app1Start = offset;
|
|
@@ -386,14 +453,14 @@ function resetAndGetOrientation(arrayBuffer) {
|
|
|
386
453
|
}
|
|
387
454
|
}
|
|
388
455
|
if (app1Start) {
|
|
389
|
-
|
|
390
|
-
|
|
456
|
+
var exifIDCode = app1Start + 4;
|
|
457
|
+
var tiffOffset = app1Start + 10;
|
|
391
458
|
if (getStringFromCharCode(dataView, exifIDCode, 4) === 'Exif') {
|
|
392
|
-
|
|
459
|
+
var endianness = dataView.getUint16(tiffOffset);
|
|
393
460
|
littleEndian = endianness === 0x4949;
|
|
394
461
|
if (littleEndian || endianness === 0x4D4D /* bigEndian */) {
|
|
395
462
|
if (dataView.getUint16(tiffOffset + 2, littleEndian) === 0x002A) {
|
|
396
|
-
|
|
463
|
+
var firstIFDOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
|
|
397
464
|
if (firstIFDOffset >= 0x00000008) {
|
|
398
465
|
ifdStart = tiffOffset + firstIFDOffset;
|
|
399
466
|
}
|
|
@@ -402,20 +469,20 @@ function resetAndGetOrientation(arrayBuffer) {
|
|
|
402
469
|
}
|
|
403
470
|
}
|
|
404
471
|
if (ifdStart) {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
for (i = 0; i <
|
|
409
|
-
|
|
410
|
-
if (dataView.getUint16(
|
|
472
|
+
var _length = dataView.getUint16(ifdStart, littleEndian);
|
|
473
|
+
var _offset;
|
|
474
|
+
var i;
|
|
475
|
+
for (i = 0; i < _length; i += 1) {
|
|
476
|
+
_offset = ifdStart + i * 12 + 2;
|
|
477
|
+
if (dataView.getUint16(_offset, littleEndian) === 0x0112 /* Orientation */) {
|
|
411
478
|
// 8 is the offset of the current tag's value
|
|
412
|
-
|
|
479
|
+
_offset += 8;
|
|
413
480
|
|
|
414
481
|
// Get the original orientation value
|
|
415
|
-
orientation = dataView.getUint16(
|
|
482
|
+
orientation = dataView.getUint16(_offset, littleEndian);
|
|
416
483
|
|
|
417
484
|
// Override the orientation with its default value
|
|
418
|
-
dataView.setUint16(
|
|
485
|
+
dataView.setUint16(_offset, 1, littleEndian);
|
|
419
486
|
break;
|
|
420
487
|
}
|
|
421
488
|
}
|
|
@@ -432,9 +499,9 @@ function resetAndGetOrientation(arrayBuffer) {
|
|
|
432
499
|
* @returns {Object} The parsed result.
|
|
433
500
|
*/
|
|
434
501
|
function parseOrientation(orientation) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
502
|
+
var rotate = 0;
|
|
503
|
+
var scaleX = 1;
|
|
504
|
+
var scaleY = 1;
|
|
438
505
|
switch (orientation) {
|
|
439
506
|
// Flip horizontal
|
|
440
507
|
case 2:
|
|
@@ -474,12 +541,12 @@ function parseOrientation(orientation) {
|
|
|
474
541
|
break;
|
|
475
542
|
}
|
|
476
543
|
return {
|
|
477
|
-
rotate,
|
|
478
|
-
scaleX,
|
|
479
|
-
scaleY
|
|
544
|
+
rotate: rotate,
|
|
545
|
+
scaleX: scaleX,
|
|
546
|
+
scaleY: scaleY
|
|
480
547
|
};
|
|
481
548
|
}
|
|
482
|
-
|
|
549
|
+
var REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/;
|
|
483
550
|
|
|
484
551
|
/**
|
|
485
552
|
* Normalize decimal number.
|
|
@@ -489,7 +556,7 @@ const REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/;
|
|
|
489
556
|
* @returns {number} Returns the normalized number.
|
|
490
557
|
*/
|
|
491
558
|
function normalizeDecimalNumber(value) {
|
|
492
|
-
|
|
559
|
+
var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100000000000;
|
|
493
560
|
return REGEXP_DECIMALS.test(value) ? Math.round(value * times) / times : value;
|
|
494
561
|
}
|
|
495
562
|
|
|
@@ -500,16 +567,14 @@ function normalizeDecimalNumber(value) {
|
|
|
500
567
|
* @returns {Object} The result sizes.
|
|
501
568
|
*/
|
|
502
569
|
function getAdjustedSizes(_ref) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
const isValidWidth = isPositiveNumber(width);
|
|
510
|
-
const isValidHeight = isPositiveNumber(height);
|
|
570
|
+
var aspectRatio = _ref.aspectRatio,
|
|
571
|
+
height = _ref.height,
|
|
572
|
+
width = _ref.width;
|
|
573
|
+
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'none';
|
|
574
|
+
var isValidWidth = isPositiveNumber(width);
|
|
575
|
+
var isValidHeight = isPositiveNumber(height);
|
|
511
576
|
if (isValidWidth && isValidHeight) {
|
|
512
|
-
|
|
577
|
+
var adjustedWidth = height * aspectRatio;
|
|
513
578
|
if ((type === 'contain' || type === 'none') && adjustedWidth > width || type === 'cover' && adjustedWidth < width) {
|
|
514
579
|
height = width / aspectRatio;
|
|
515
580
|
} else {
|
|
@@ -521,8 +586,8 @@ function getAdjustedSizes(_ref) {
|
|
|
521
586
|
width = height * aspectRatio;
|
|
522
587
|
}
|
|
523
588
|
return {
|
|
524
|
-
width,
|
|
525
|
-
height
|
|
589
|
+
width: width,
|
|
590
|
+
height: height
|
|
526
591
|
};
|
|
527
592
|
}
|
|
528
593
|
|
|
@@ -532,15 +597,13 @@ function getAdjustedSizes(_ref) {
|
|
|
532
597
|
* @returns {Array} The read Exif information.
|
|
533
598
|
*/
|
|
534
599
|
function getExif(arrayBuffer) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
const segments = [];
|
|
540
|
-
let start = 0;
|
|
600
|
+
var array = toArray(new Uint8Array(arrayBuffer));
|
|
601
|
+
var length = array.length;
|
|
602
|
+
var segments = [];
|
|
603
|
+
var start = 0;
|
|
541
604
|
while (start + 3 < length) {
|
|
542
|
-
|
|
543
|
-
|
|
605
|
+
var value = array[start];
|
|
606
|
+
var next = array[start + 1];
|
|
544
607
|
|
|
545
608
|
// SOS (Start of Scan)
|
|
546
609
|
if (value === 0xFF && next === 0xDA) {
|
|
@@ -551,14 +614,14 @@ function getExif(arrayBuffer) {
|
|
|
551
614
|
if (value === 0xFF && next === 0xD8) {
|
|
552
615
|
start += 2;
|
|
553
616
|
} else {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
617
|
+
var offset = array[start + 2] * 256 + array[start + 3];
|
|
618
|
+
var end = start + offset + 2;
|
|
619
|
+
var segment = array.slice(start, end);
|
|
557
620
|
segments.push(segment);
|
|
558
621
|
start = end;
|
|
559
622
|
}
|
|
560
623
|
}
|
|
561
|
-
return segments.reduce((exifArray, current)
|
|
624
|
+
return segments.reduce(function (exifArray, current) {
|
|
562
625
|
if (current[0] === 0xFF && current[1] === 0xE1) {
|
|
563
626
|
return exifArray.concat(current);
|
|
564
627
|
}
|
|
@@ -573,414 +636,420 @@ function getExif(arrayBuffer) {
|
|
|
573
636
|
* @returns {ArrayBuffer} The transformed array buffer.
|
|
574
637
|
*/
|
|
575
638
|
function insertExif(arrayBuffer, exifArray) {
|
|
576
|
-
|
|
639
|
+
var array = toArray(new Uint8Array(arrayBuffer));
|
|
577
640
|
if (array[2] !== 0xFF || array[3] !== 0xE0) {
|
|
578
641
|
return arrayBuffer;
|
|
579
642
|
}
|
|
580
|
-
|
|
581
|
-
|
|
643
|
+
var app0Length = array[4] * 256 + array[5];
|
|
644
|
+
var newArrayBuffer = [0xFF, 0xD8].concat(exifArray, array.slice(4 + app0Length));
|
|
582
645
|
return new Uint8Array(newArrayBuffer);
|
|
583
646
|
}
|
|
584
647
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
const REGEXP_EXTENSION = /\.\w+$/;
|
|
591
|
-
const AnotherCompressor = WINDOW.Compressor;
|
|
648
|
+
var ArrayBuffer$1 = WINDOW.ArrayBuffer,
|
|
649
|
+
FileReader = WINDOW.FileReader;
|
|
650
|
+
var URL = WINDOW.URL || WINDOW.webkitURL;
|
|
651
|
+
var REGEXP_EXTENSION = /\.\w+$/;
|
|
652
|
+
var AnotherCompressor = WINDOW.Compressor;
|
|
592
653
|
|
|
593
654
|
/**
|
|
594
655
|
* Creates a new image compressor.
|
|
595
656
|
* @class
|
|
596
657
|
*/
|
|
597
|
-
|
|
658
|
+
var Compressor = /*#__PURE__*/function () {
|
|
598
659
|
/**
|
|
599
660
|
* The constructor of Compressor.
|
|
600
661
|
* @param {File|Blob} file - The target image file for compressing.
|
|
601
662
|
* @param {Object} [options] - The options for compressing.
|
|
602
663
|
*/
|
|
603
|
-
|
|
664
|
+
function Compressor(file, options) {
|
|
665
|
+
_classCallCheck(this, Compressor);
|
|
604
666
|
this.file = file;
|
|
605
667
|
this.exif = [];
|
|
606
668
|
this.image = new Image();
|
|
607
|
-
this.options = {
|
|
608
|
-
...DEFAULTS,
|
|
609
|
-
...options
|
|
610
|
-
};
|
|
669
|
+
this.options = _objectSpread2(_objectSpread2({}, DEFAULTS), options);
|
|
611
670
|
this.aborted = false;
|
|
612
671
|
this.result = null;
|
|
613
672
|
this.init();
|
|
614
673
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
target
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
if (orientation > 1) {
|
|
661
|
-
_extends(data, parseOrientation(orientation));
|
|
674
|
+
_createClass(Compressor, [{
|
|
675
|
+
key: "init",
|
|
676
|
+
value: function init() {
|
|
677
|
+
var _this = this;
|
|
678
|
+
var file = this.file,
|
|
679
|
+
options = this.options;
|
|
680
|
+
if (!isBlob(file)) {
|
|
681
|
+
this.fail(new Error('The first argument must be a File or Blob object.'));
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
var mimeType = file.type;
|
|
685
|
+
if (!isImageType(mimeType)) {
|
|
686
|
+
this.fail(new Error('The first argument must be an image File or Blob object.'));
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
if (!URL || !FileReader) {
|
|
690
|
+
this.fail(new Error('The current browser does not support image compression.'));
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (!ArrayBuffer$1) {
|
|
694
|
+
options.checkOrientation = false;
|
|
695
|
+
options.retainExif = false;
|
|
696
|
+
}
|
|
697
|
+
var isJPEGImage = mimeType === 'image/jpeg';
|
|
698
|
+
var checkOrientation = isJPEGImage && options.checkOrientation;
|
|
699
|
+
var retainExif = isJPEGImage && options.retainExif;
|
|
700
|
+
if (URL && !checkOrientation && !retainExif) {
|
|
701
|
+
this.load({
|
|
702
|
+
url: URL.createObjectURL(file)
|
|
703
|
+
});
|
|
704
|
+
} else {
|
|
705
|
+
var reader = new FileReader();
|
|
706
|
+
this.reader = reader;
|
|
707
|
+
reader.onload = function (_ref) {
|
|
708
|
+
var target = _ref.target;
|
|
709
|
+
var result = target.result;
|
|
710
|
+
var data = {};
|
|
711
|
+
var orientation = 1;
|
|
712
|
+
if (checkOrientation) {
|
|
713
|
+
// Reset the orientation value to its default value 1
|
|
714
|
+
// as some iOS browsers will render image with its orientation
|
|
715
|
+
orientation = resetAndGetOrientation(result);
|
|
716
|
+
if (orientation > 1) {
|
|
717
|
+
_extends(data, parseOrientation(orientation));
|
|
718
|
+
}
|
|
662
719
|
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
if (!URL
|
|
720
|
+
if (retainExif) {
|
|
721
|
+
_this.exif = getExif(result);
|
|
722
|
+
}
|
|
723
|
+
if (checkOrientation || retainExif) {
|
|
724
|
+
if (!URL
|
|
669
725
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
726
|
+
// Generate a new URL with the default orientation value 1.
|
|
727
|
+
|| orientation > 1) {
|
|
728
|
+
data.url = arrayBufferToDataURL(result, mimeType);
|
|
729
|
+
} else {
|
|
730
|
+
data.url = URL.createObjectURL(file);
|
|
731
|
+
}
|
|
673
732
|
} else {
|
|
674
|
-
data.url =
|
|
733
|
+
data.url = result;
|
|
675
734
|
}
|
|
735
|
+
_this.load(data);
|
|
736
|
+
};
|
|
737
|
+
reader.onabort = function () {
|
|
738
|
+
_this.fail(new Error('Aborted to read the image with FileReader.'));
|
|
739
|
+
};
|
|
740
|
+
reader.onerror = function () {
|
|
741
|
+
_this.fail(new Error('Failed to read the image with FileReader.'));
|
|
742
|
+
};
|
|
743
|
+
reader.onloadend = function () {
|
|
744
|
+
_this.reader = null;
|
|
745
|
+
};
|
|
746
|
+
if (checkOrientation || retainExif) {
|
|
747
|
+
reader.readAsArrayBuffer(file);
|
|
676
748
|
} else {
|
|
677
|
-
|
|
749
|
+
reader.readAsDataURL(file);
|
|
678
750
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}, {
|
|
754
|
+
key: "load",
|
|
755
|
+
value: function load(data) {
|
|
756
|
+
var _this2 = this;
|
|
757
|
+
var file = this.file,
|
|
758
|
+
image = this.image;
|
|
759
|
+
image.onload = function () {
|
|
760
|
+
_this2.draw(_objectSpread2(_objectSpread2({}, data), {}, {
|
|
761
|
+
naturalWidth: image.naturalWidth,
|
|
762
|
+
naturalHeight: image.naturalHeight
|
|
763
|
+
}));
|
|
683
764
|
};
|
|
684
|
-
|
|
685
|
-
|
|
765
|
+
image.onabort = function () {
|
|
766
|
+
_this2.fail(new Error('Aborted to load the image.'));
|
|
686
767
|
};
|
|
687
|
-
|
|
688
|
-
|
|
768
|
+
image.onerror = function () {
|
|
769
|
+
_this2.fail(new Error('Failed to load the image.'));
|
|
689
770
|
};
|
|
690
|
-
if (checkOrientation || retainExif) {
|
|
691
|
-
reader.readAsArrayBuffer(file);
|
|
692
|
-
} else {
|
|
693
|
-
reader.readAsDataURL(file);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
load(data) {
|
|
698
|
-
const {
|
|
699
|
-
file,
|
|
700
|
-
image
|
|
701
|
-
} = this;
|
|
702
|
-
image.onload = () => {
|
|
703
|
-
this.draw({
|
|
704
|
-
...data,
|
|
705
|
-
naturalWidth: image.naturalWidth,
|
|
706
|
-
naturalHeight: image.naturalHeight
|
|
707
|
-
});
|
|
708
|
-
};
|
|
709
|
-
image.onabort = () => {
|
|
710
|
-
this.fail(new Error('Aborted to load the image.'));
|
|
711
|
-
};
|
|
712
|
-
image.onerror = () => {
|
|
713
|
-
this.fail(new Error('Failed to load the image.'));
|
|
714
|
-
};
|
|
715
771
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
}
|
|
725
|
-
draw(_ref2) {
|
|
726
|
-
let {
|
|
727
|
-
naturalWidth,
|
|
728
|
-
naturalHeight,
|
|
729
|
-
rotate = 0,
|
|
730
|
-
scaleX = 1,
|
|
731
|
-
scaleY = 1
|
|
732
|
-
} = _ref2;
|
|
733
|
-
const {
|
|
734
|
-
file,
|
|
735
|
-
image,
|
|
736
|
-
options
|
|
737
|
-
} = this;
|
|
738
|
-
const canvas = document.createElement('canvas');
|
|
739
|
-
const context = canvas.getContext('2d');
|
|
740
|
-
const is90DegreesRotated = Math.abs(rotate) % 180 === 90;
|
|
741
|
-
const resizable = (options.resize === 'contain' || options.resize === 'cover') && isPositiveNumber(options.width) && isPositiveNumber(options.height);
|
|
742
|
-
let maxWidth = Math.max(options.maxWidth, 0) || Infinity;
|
|
743
|
-
let maxHeight = Math.max(options.maxHeight, 0) || Infinity;
|
|
744
|
-
let minWidth = Math.max(options.minWidth, 0) || 0;
|
|
745
|
-
let minHeight = Math.max(options.minHeight, 0) || 0;
|
|
746
|
-
let aspectRatio = naturalWidth / naturalHeight;
|
|
747
|
-
let {
|
|
748
|
-
width,
|
|
749
|
-
height
|
|
750
|
-
} = options;
|
|
751
|
-
if (is90DegreesRotated) {
|
|
752
|
-
[maxWidth, maxHeight] = [maxHeight, maxWidth];
|
|
753
|
-
[minWidth, minHeight] = [minHeight, minWidth];
|
|
754
|
-
[width, height] = [height, width];
|
|
755
|
-
}
|
|
756
|
-
if (resizable) {
|
|
757
|
-
aspectRatio = width / height;
|
|
758
|
-
}
|
|
759
|
-
({
|
|
760
|
-
width: maxWidth,
|
|
761
|
-
height: maxHeight
|
|
762
|
-
} = getAdjustedSizes({
|
|
763
|
-
aspectRatio,
|
|
764
|
-
width: maxWidth,
|
|
765
|
-
height: maxHeight
|
|
766
|
-
}, 'contain'));
|
|
767
|
-
({
|
|
768
|
-
width: minWidth,
|
|
769
|
-
height: minHeight
|
|
770
|
-
} = getAdjustedSizes({
|
|
771
|
-
aspectRatio,
|
|
772
|
-
width: minWidth,
|
|
773
|
-
height: minHeight
|
|
774
|
-
}, 'cover'));
|
|
775
|
-
if (resizable) {
|
|
776
|
-
({
|
|
777
|
-
width,
|
|
778
|
-
height
|
|
779
|
-
} = getAdjustedSizes({
|
|
780
|
-
aspectRatio,
|
|
781
|
-
width,
|
|
782
|
-
height
|
|
783
|
-
}, options.resize));
|
|
784
|
-
} else {
|
|
785
|
-
({
|
|
786
|
-
width = naturalWidth,
|
|
787
|
-
height = naturalHeight
|
|
788
|
-
} = getAdjustedSizes({
|
|
789
|
-
aspectRatio,
|
|
790
|
-
width,
|
|
791
|
-
height
|
|
792
|
-
}));
|
|
793
|
-
}
|
|
794
|
-
width = Math.floor(normalizeDecimalNumber(Math.min(Math.max(width, minWidth), maxWidth)));
|
|
795
|
-
height = Math.floor(normalizeDecimalNumber(Math.min(Math.max(height, minHeight), maxHeight)));
|
|
796
|
-
const destX = -width / 2;
|
|
797
|
-
const destY = -height / 2;
|
|
798
|
-
const destWidth = width;
|
|
799
|
-
const destHeight = height;
|
|
800
|
-
const params = [];
|
|
801
|
-
if (resizable) {
|
|
802
|
-
let srcX = 0;
|
|
803
|
-
let srcY = 0;
|
|
804
|
-
let srcWidth = naturalWidth;
|
|
805
|
-
let srcHeight = naturalHeight;
|
|
806
|
-
({
|
|
807
|
-
width: srcWidth,
|
|
808
|
-
height: srcHeight
|
|
809
|
-
} = getAdjustedSizes({
|
|
810
|
-
aspectRatio,
|
|
811
|
-
width: naturalWidth,
|
|
812
|
-
height: naturalHeight
|
|
813
|
-
}, {
|
|
814
|
-
contain: 'cover',
|
|
815
|
-
cover: 'contain'
|
|
816
|
-
}[options.resize]));
|
|
817
|
-
srcX = (naturalWidth - srcWidth) / 2;
|
|
818
|
-
srcY = (naturalHeight - srcHeight) / 2;
|
|
819
|
-
params.push(srcX, srcY, srcWidth, srcHeight);
|
|
820
|
-
}
|
|
821
|
-
params.push(destX, destY, destWidth, destHeight);
|
|
822
|
-
if (is90DegreesRotated) {
|
|
823
|
-
[width, height] = [height, width];
|
|
824
|
-
}
|
|
825
|
-
canvas.width = width;
|
|
826
|
-
canvas.height = height;
|
|
827
|
-
if (!isImageType(options.mimeType)) {
|
|
828
|
-
options.mimeType = file.type;
|
|
772
|
+
// Match all browsers that use WebKit as the layout engine in iOS devices,
|
|
773
|
+
// such as Safari for iOS, Chrome for iOS, and in-app browsers.
|
|
774
|
+
if (WINDOW.navigator && /(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(WINDOW.navigator.userAgent)) {
|
|
775
|
+
// Fix the `The operation is insecure` error (#57)
|
|
776
|
+
image.crossOrigin = 'anonymous';
|
|
777
|
+
}
|
|
778
|
+
image.alt = file.name;
|
|
779
|
+
image.src = data.url;
|
|
829
780
|
}
|
|
830
|
-
|
|
781
|
+
}, {
|
|
782
|
+
key: "draw",
|
|
783
|
+
value: function draw(_ref2) {
|
|
784
|
+
var _this3 = this;
|
|
785
|
+
var naturalWidth = _ref2.naturalWidth,
|
|
786
|
+
naturalHeight = _ref2.naturalHeight,
|
|
787
|
+
_ref2$rotate = _ref2.rotate,
|
|
788
|
+
rotate = _ref2$rotate === void 0 ? 0 : _ref2$rotate,
|
|
789
|
+
_ref2$scaleX = _ref2.scaleX,
|
|
790
|
+
scaleX = _ref2$scaleX === void 0 ? 1 : _ref2$scaleX,
|
|
791
|
+
_ref2$scaleY = _ref2.scaleY,
|
|
792
|
+
scaleY = _ref2$scaleY === void 0 ? 1 : _ref2$scaleY;
|
|
793
|
+
var file = this.file,
|
|
794
|
+
image = this.image,
|
|
795
|
+
options = this.options;
|
|
796
|
+
var canvas = document.createElement('canvas');
|
|
797
|
+
var context = canvas.getContext('2d');
|
|
798
|
+
var is90DegreesRotated = Math.abs(rotate) % 180 === 90;
|
|
799
|
+
var resizable = (options.resize === 'contain' || options.resize === 'cover') && isPositiveNumber(options.width) && isPositiveNumber(options.height);
|
|
800
|
+
var maxWidth = Math.max(options.maxWidth, 0) || Infinity;
|
|
801
|
+
var maxHeight = Math.max(options.maxHeight, 0) || Infinity;
|
|
802
|
+
var minWidth = Math.max(options.minWidth, 0) || 0;
|
|
803
|
+
var minHeight = Math.max(options.minHeight, 0) || 0;
|
|
804
|
+
var aspectRatio = naturalWidth / naturalHeight;
|
|
805
|
+
var width = options.width,
|
|
806
|
+
height = options.height;
|
|
807
|
+
if (is90DegreesRotated) {
|
|
808
|
+
var _ref3 = [maxHeight, maxWidth];
|
|
809
|
+
maxWidth = _ref3[0];
|
|
810
|
+
maxHeight = _ref3[1];
|
|
811
|
+
var _ref4 = [minHeight, minWidth];
|
|
812
|
+
minWidth = _ref4[0];
|
|
813
|
+
minHeight = _ref4[1];
|
|
814
|
+
var _ref5 = [height, width];
|
|
815
|
+
width = _ref5[0];
|
|
816
|
+
height = _ref5[1];
|
|
817
|
+
}
|
|
818
|
+
if (resizable) {
|
|
819
|
+
aspectRatio = width / height;
|
|
820
|
+
}
|
|
821
|
+
var _getAdjustedSizes = getAdjustedSizes({
|
|
822
|
+
aspectRatio: aspectRatio,
|
|
823
|
+
width: maxWidth,
|
|
824
|
+
height: maxHeight
|
|
825
|
+
}, 'contain');
|
|
826
|
+
maxWidth = _getAdjustedSizes.width;
|
|
827
|
+
maxHeight = _getAdjustedSizes.height;
|
|
828
|
+
var _getAdjustedSizes2 = getAdjustedSizes({
|
|
829
|
+
aspectRatio: aspectRatio,
|
|
830
|
+
width: minWidth,
|
|
831
|
+
height: minHeight
|
|
832
|
+
}, 'cover');
|
|
833
|
+
minWidth = _getAdjustedSizes2.width;
|
|
834
|
+
minHeight = _getAdjustedSizes2.height;
|
|
835
|
+
if (resizable) {
|
|
836
|
+
var _getAdjustedSizes3 = getAdjustedSizes({
|
|
837
|
+
aspectRatio: aspectRatio,
|
|
838
|
+
width: width,
|
|
839
|
+
height: height
|
|
840
|
+
}, options.resize);
|
|
841
|
+
width = _getAdjustedSizes3.width;
|
|
842
|
+
height = _getAdjustedSizes3.height;
|
|
843
|
+
} else {
|
|
844
|
+
var _getAdjustedSizes4 = getAdjustedSizes({
|
|
845
|
+
aspectRatio: aspectRatio,
|
|
846
|
+
width: width,
|
|
847
|
+
height: height
|
|
848
|
+
});
|
|
849
|
+
var _getAdjustedSizes4$wi = _getAdjustedSizes4.width;
|
|
850
|
+
width = _getAdjustedSizes4$wi === void 0 ? naturalWidth : _getAdjustedSizes4$wi;
|
|
851
|
+
var _getAdjustedSizes4$he = _getAdjustedSizes4.height;
|
|
852
|
+
height = _getAdjustedSizes4$he === void 0 ? naturalHeight : _getAdjustedSizes4$he;
|
|
853
|
+
}
|
|
854
|
+
width = Math.floor(normalizeDecimalNumber(Math.min(Math.max(width, minWidth), maxWidth)));
|
|
855
|
+
height = Math.floor(normalizeDecimalNumber(Math.min(Math.max(height, minHeight), maxHeight)));
|
|
856
|
+
var destX = -width / 2;
|
|
857
|
+
var destY = -height / 2;
|
|
858
|
+
var destWidth = width;
|
|
859
|
+
var destHeight = height;
|
|
860
|
+
var params = [];
|
|
861
|
+
if (resizable) {
|
|
862
|
+
var srcX = 0;
|
|
863
|
+
var srcY = 0;
|
|
864
|
+
var srcWidth = naturalWidth;
|
|
865
|
+
var srcHeight = naturalHeight;
|
|
866
|
+
var _getAdjustedSizes5 = getAdjustedSizes({
|
|
867
|
+
aspectRatio: aspectRatio,
|
|
868
|
+
width: naturalWidth,
|
|
869
|
+
height: naturalHeight
|
|
870
|
+
}, {
|
|
871
|
+
contain: 'cover',
|
|
872
|
+
cover: 'contain'
|
|
873
|
+
}[options.resize]);
|
|
874
|
+
srcWidth = _getAdjustedSizes5.width;
|
|
875
|
+
srcHeight = _getAdjustedSizes5.height;
|
|
876
|
+
srcX = (naturalWidth - srcWidth) / 2;
|
|
877
|
+
srcY = (naturalHeight - srcHeight) / 2;
|
|
878
|
+
params.push(srcX, srcY, srcWidth, srcHeight);
|
|
879
|
+
}
|
|
880
|
+
params.push(destX, destY, destWidth, destHeight);
|
|
881
|
+
if (is90DegreesRotated) {
|
|
882
|
+
var _ref6 = [height, width];
|
|
883
|
+
width = _ref6[0];
|
|
884
|
+
height = _ref6[1];
|
|
885
|
+
}
|
|
886
|
+
canvas.width = width;
|
|
887
|
+
canvas.height = height;
|
|
888
|
+
if (!isImageType(options.mimeType)) {
|
|
889
|
+
options.mimeType = file.type;
|
|
890
|
+
}
|
|
891
|
+
var fillStyle = 'transparent';
|
|
831
892
|
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
893
|
+
// Converts PNG files over the `convertSize` to JPEGs.
|
|
894
|
+
if (file.size > options.convertSize && options.convertTypes.indexOf(options.mimeType) >= 0) {
|
|
895
|
+
options.mimeType = 'image/jpeg';
|
|
896
|
+
}
|
|
897
|
+
var isJPEGImage = options.mimeType === 'image/jpeg';
|
|
898
|
+
if (isJPEGImage) {
|
|
899
|
+
fillStyle = '#fff';
|
|
900
|
+
}
|
|
840
901
|
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
if (blob && isJPEGImage && options.retainExif && this.exif && this.exif.length > 0) {
|
|
870
|
-
const next = arrayBuffer => done(toBlob(arrayBufferToDataURL(insertExif(arrayBuffer, this.exif), options.mimeType)));
|
|
871
|
-
if (blob.arrayBuffer) {
|
|
872
|
-
blob.arrayBuffer().then(next).catch(() => {
|
|
873
|
-
this.fail(new Error('Failed to read the compressed image with Blob.arrayBuffer().'));
|
|
902
|
+
// Override the default fill color (#000, black)
|
|
903
|
+
context.fillStyle = fillStyle;
|
|
904
|
+
context.fillRect(0, 0, width, height);
|
|
905
|
+
if (options.beforeDraw) {
|
|
906
|
+
options.beforeDraw.call(this, context, canvas);
|
|
907
|
+
}
|
|
908
|
+
if (this.aborted) {
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
context.save();
|
|
912
|
+
context.translate(width / 2, height / 2);
|
|
913
|
+
context.rotate(rotate * Math.PI / 180);
|
|
914
|
+
context.scale(scaleX, scaleY);
|
|
915
|
+
context.drawImage.apply(context, [image].concat(params));
|
|
916
|
+
context.restore();
|
|
917
|
+
if (options.drew) {
|
|
918
|
+
options.drew.call(this, context, canvas);
|
|
919
|
+
}
|
|
920
|
+
if (this.aborted) {
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
var callback = function callback(blob) {
|
|
924
|
+
if (!_this3.aborted) {
|
|
925
|
+
var done = function done(result) {
|
|
926
|
+
return _this3.done({
|
|
927
|
+
naturalWidth: naturalWidth,
|
|
928
|
+
naturalHeight: naturalHeight,
|
|
929
|
+
result: result
|
|
874
930
|
});
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
let {
|
|
880
|
-
target
|
|
881
|
-
} = _ref3;
|
|
882
|
-
next(target.result);
|
|
883
|
-
};
|
|
884
|
-
reader.onabort = () => {
|
|
885
|
-
this.fail(new Error('Aborted to read the compressed image with FileReader.'));
|
|
886
|
-
};
|
|
887
|
-
reader.onerror = () => {
|
|
888
|
-
this.fail(new Error('Failed to read the compressed image with FileReader.'));
|
|
889
|
-
};
|
|
890
|
-
reader.onloadend = () => {
|
|
891
|
-
this.reader = null;
|
|
931
|
+
};
|
|
932
|
+
if (blob && isJPEGImage && options.retainExif && _this3.exif && _this3.exif.length > 0) {
|
|
933
|
+
var next = function next(arrayBuffer) {
|
|
934
|
+
return done(toBlob(arrayBufferToDataURL(insertExif(arrayBuffer, _this3.exif), options.mimeType)));
|
|
892
935
|
};
|
|
893
|
-
|
|
936
|
+
if (blob.arrayBuffer) {
|
|
937
|
+
blob.arrayBuffer().then(next).catch(function () {
|
|
938
|
+
_this3.fail(new Error('Failed to read the compressed image with Blob.arrayBuffer().'));
|
|
939
|
+
});
|
|
940
|
+
} else {
|
|
941
|
+
var reader = new FileReader();
|
|
942
|
+
_this3.reader = reader;
|
|
943
|
+
reader.onload = function (_ref7) {
|
|
944
|
+
var target = _ref7.target;
|
|
945
|
+
next(target.result);
|
|
946
|
+
};
|
|
947
|
+
reader.onabort = function () {
|
|
948
|
+
_this3.fail(new Error('Aborted to read the compressed image with FileReader.'));
|
|
949
|
+
};
|
|
950
|
+
reader.onerror = function () {
|
|
951
|
+
_this3.fail(new Error('Failed to read the compressed image with FileReader.'));
|
|
952
|
+
};
|
|
953
|
+
reader.onloadend = function () {
|
|
954
|
+
_this3.reader = null;
|
|
955
|
+
};
|
|
956
|
+
reader.readAsArrayBuffer(blob);
|
|
957
|
+
}
|
|
958
|
+
} else {
|
|
959
|
+
done(blob);
|
|
894
960
|
}
|
|
895
|
-
} else {
|
|
896
|
-
done(blob);
|
|
897
961
|
}
|
|
962
|
+
};
|
|
963
|
+
if (canvas.toBlob) {
|
|
964
|
+
canvas.toBlob(callback, options.mimeType, options.quality);
|
|
965
|
+
} else {
|
|
966
|
+
callback(toBlob(canvas.toDataURL(options.mimeType, options.quality)));
|
|
898
967
|
}
|
|
899
|
-
};
|
|
900
|
-
if (canvas.toBlob) {
|
|
901
|
-
canvas.toBlob(callback, options.mimeType, options.quality);
|
|
902
|
-
} else {
|
|
903
|
-
callback(toBlob(canvas.toDataURL(options.mimeType, options.quality)));
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
done(_ref4) {
|
|
907
|
-
let {
|
|
908
|
-
naturalWidth,
|
|
909
|
-
naturalHeight,
|
|
910
|
-
result
|
|
911
|
-
} = _ref4;
|
|
912
|
-
const {
|
|
913
|
-
file,
|
|
914
|
-
image,
|
|
915
|
-
options
|
|
916
|
-
} = this;
|
|
917
|
-
if (URL && image.src.indexOf('blob:') === 0) {
|
|
918
|
-
URL.revokeObjectURL(image.src);
|
|
919
968
|
}
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
969
|
+
}, {
|
|
970
|
+
key: "done",
|
|
971
|
+
value: function done(_ref8) {
|
|
972
|
+
var naturalWidth = _ref8.naturalWidth,
|
|
973
|
+
naturalHeight = _ref8.naturalHeight,
|
|
974
|
+
result = _ref8.result;
|
|
975
|
+
var file = this.file,
|
|
976
|
+
image = this.image,
|
|
977
|
+
options = this.options;
|
|
978
|
+
if (URL && image.src.indexOf('blob:') === 0) {
|
|
979
|
+
URL.revokeObjectURL(image.src);
|
|
980
|
+
}
|
|
981
|
+
if (result) {
|
|
982
|
+
// Returns original file if the result is greater than it and without size related options
|
|
983
|
+
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
|
+
result = file;
|
|
985
|
+
} else {
|
|
986
|
+
var date = new Date();
|
|
987
|
+
result.lastModified = date.getTime();
|
|
988
|
+
result.lastModifiedDate = date;
|
|
989
|
+
result.name = file.name;
|
|
990
|
+
|
|
991
|
+
// 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));
|
|
994
|
+
}
|
|
933
995
|
}
|
|
996
|
+
} else {
|
|
997
|
+
// Returns original file if the result is null in some cases.
|
|
998
|
+
result = file;
|
|
999
|
+
}
|
|
1000
|
+
this.result = result;
|
|
1001
|
+
if (options.success) {
|
|
1002
|
+
options.success.call(this, result);
|
|
934
1003
|
}
|
|
935
|
-
} else {
|
|
936
|
-
// Returns original file if the result is null in some cases.
|
|
937
|
-
result = file;
|
|
938
|
-
}
|
|
939
|
-
this.result = result;
|
|
940
|
-
if (options.success) {
|
|
941
|
-
options.success.call(this, result);
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
fail(err) {
|
|
945
|
-
const {
|
|
946
|
-
options
|
|
947
|
-
} = this;
|
|
948
|
-
if (options.error) {
|
|
949
|
-
options.error.call(this, err);
|
|
950
|
-
} else {
|
|
951
|
-
throw err;
|
|
952
1004
|
}
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
if (
|
|
958
|
-
|
|
959
|
-
} else if (!this.image.complete) {
|
|
960
|
-
this.image.onload = null;
|
|
961
|
-
this.image.onabort();
|
|
1005
|
+
}, {
|
|
1006
|
+
key: "fail",
|
|
1007
|
+
value: function fail(err) {
|
|
1008
|
+
var options = this.options;
|
|
1009
|
+
if (options.error) {
|
|
1010
|
+
options.error.call(this, err);
|
|
962
1011
|
} else {
|
|
963
|
-
|
|
1012
|
+
throw err;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}, {
|
|
1016
|
+
key: "abort",
|
|
1017
|
+
value: function abort() {
|
|
1018
|
+
if (!this.aborted) {
|
|
1019
|
+
this.aborted = true;
|
|
1020
|
+
if (this.reader) {
|
|
1021
|
+
this.reader.abort();
|
|
1022
|
+
} else if (!this.image.complete) {
|
|
1023
|
+
this.image.onload = null;
|
|
1024
|
+
this.image.onabort();
|
|
1025
|
+
} else {
|
|
1026
|
+
this.fail(new Error('The compression process has been aborted.'));
|
|
1027
|
+
}
|
|
964
1028
|
}
|
|
965
1029
|
}
|
|
966
|
-
}
|
|
967
1030
|
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
1031
|
+
/**
|
|
1032
|
+
* Get the no conflict compressor class.
|
|
1033
|
+
* @returns {Compressor} The compressor class.
|
|
1034
|
+
*/
|
|
1035
|
+
}], [{
|
|
1036
|
+
key: "noConflict",
|
|
1037
|
+
value: function noConflict() {
|
|
1038
|
+
window.Compressor = AnotherCompressor;
|
|
1039
|
+
return Compressor;
|
|
1040
|
+
}
|
|
976
1041
|
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1042
|
+
/**
|
|
1043
|
+
* Change the default options.
|
|
1044
|
+
* @param {Object} options - The new default options.
|
|
1045
|
+
*/
|
|
1046
|
+
}, {
|
|
1047
|
+
key: "setDefaults",
|
|
1048
|
+
value: function setDefaults(options) {
|
|
1049
|
+
_extends(DEFAULTS, options);
|
|
1050
|
+
}
|
|
1051
|
+
}]);
|
|
1052
|
+
return Compressor;
|
|
1053
|
+
}();
|
|
985
1054
|
|
|
986
1055
|
export { Compressor as default };
|