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