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