450f1084ad7ff1924da53bcb3b2fa22dbc252ea4.svn-base 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*!
  2. * Compressor.js v1.0.5
  3. * https://fengyuanchen.github.io/compressorjs
  4. *
  5. * Copyright 2018-present Chen Fengyuan
  6. * Released under the MIT license
  7. *
  8. * Date: 2019-01-23T10:53:08.724Z
  9. */
  10. (function (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  12. typeof define === 'function' && define.amd ? define(factory) :
  13. (global = global || self, global.Compressor = factory());
  14. }(this, function () { 'use strict';
  15. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. function _defineProperties(target, props) {
  21. for (var i = 0; i < props.length; i++) {
  22. var descriptor = props[i];
  23. descriptor.enumerable = descriptor.enumerable || false;
  24. descriptor.configurable = true;
  25. if ("value" in descriptor) descriptor.writable = true;
  26. Object.defineProperty(target, descriptor.key, descriptor);
  27. }
  28. }
  29. function _createClass(Constructor, protoProps, staticProps) {
  30. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  31. if (staticProps) _defineProperties(Constructor, staticProps);
  32. return Constructor;
  33. }
  34. function _defineProperty(obj, key, value) {
  35. if (key in obj) {
  36. Object.defineProperty(obj, key, {
  37. value: value,
  38. enumerable: true,
  39. configurable: true,
  40. writable: true
  41. });
  42. } else {
  43. obj[key] = value;
  44. }
  45. return obj;
  46. }
  47. function _extends() {
  48. _extends = Object.assign || function (target) {
  49. for (var i = 1; i < arguments.length; i++) {
  50. var source = arguments[i];
  51. for (var key in source) {
  52. if (Object.prototype.hasOwnProperty.call(source, key)) {
  53. target[key] = source[key];
  54. }
  55. }
  56. }
  57. return target;
  58. };
  59. return _extends.apply(this, arguments);
  60. }
  61. function _objectSpread(target) {
  62. for (var i = 1; i < arguments.length; i++) {
  63. var source = arguments[i] != null ? arguments[i] : {};
  64. var ownKeys = Object.keys(source);
  65. if (typeof Object.getOwnPropertySymbols === 'function') {
  66. ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
  67. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  68. }));
  69. }
  70. ownKeys.forEach(function (key) {
  71. _defineProperty(target, key, source[key]);
  72. });
  73. }
  74. return target;
  75. }
  76. function createCommonjsModule(fn, module) {
  77. return module = { exports: {} }, fn(module, module.exports), module.exports;
  78. }
  79. var canvasToBlob = createCommonjsModule(function (module) {
  80. if (typeof window === 'undefined') {
  81. return;
  82. }
  83. (function (window) {
  84. var CanvasPrototype = window.HTMLCanvasElement && window.HTMLCanvasElement.prototype;
  85. var hasBlobConstructor = window.Blob && function () {
  86. try {
  87. return Boolean(new Blob());
  88. } catch (e) {
  89. return false;
  90. }
  91. }();
  92. var hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array && function () {
  93. try {
  94. return new Blob([new Uint8Array(100)]).size === 100;
  95. } catch (e) {
  96. return false;
  97. }
  98. }();
  99. var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
  100. var dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/;
  101. var dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob && window.ArrayBuffer && window.Uint8Array && function (dataURI) {
  102. var matches, mediaType, isBase64, dataString, byteString, arrayBuffer, intArray, i, bb; // Parse the dataURI components as per RFC 2397
  103. matches = dataURI.match(dataURIPattern);
  104. if (!matches) {
  105. throw new Error('invalid data URI');
  106. } // Default to text/plain;charset=US-ASCII
  107. mediaType = matches[2] ? matches[1] : 'text/plain' + (matches[3] || ';charset=US-ASCII');
  108. isBase64 = !!matches[4];
  109. dataString = dataURI.slice(matches[0].length);
  110. if (isBase64) {
  111. // Convert base64 to raw binary data held in a string:
  112. byteString = atob(dataString);
  113. } else {
  114. // Convert base64/URLEncoded data component to raw binary:
  115. byteString = decodeURIComponent(dataString);
  116. } // Write the bytes of the string to an ArrayBuffer:
  117. arrayBuffer = new ArrayBuffer(byteString.length);
  118. intArray = new Uint8Array(arrayBuffer);
  119. for (i = 0; i < byteString.length; i += 1) {
  120. intArray[i] = byteString.charCodeAt(i);
  121. } // Write the ArrayBuffer (or ArrayBufferView) to a blob:
  122. if (hasBlobConstructor) {
  123. return new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], {
  124. type: mediaType
  125. });
  126. }
  127. bb = new BlobBuilder();
  128. bb.append(arrayBuffer);
  129. return bb.getBlob(mediaType);
  130. };
  131. if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
  132. if (CanvasPrototype.mozGetAsFile) {
  133. CanvasPrototype.toBlob = function (callback, type, quality) {
  134. var self = this;
  135. setTimeout(function () {
  136. if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
  137. callback(dataURLtoBlob(self.toDataURL(type, quality)));
  138. } else {
  139. callback(self.mozGetAsFile('blob', type));
  140. }
  141. });
  142. };
  143. } else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
  144. CanvasPrototype.toBlob = function (callback, type, quality) {
  145. var self = this;
  146. setTimeout(function () {
  147. callback(dataURLtoBlob(self.toDataURL(type, quality)));
  148. });
  149. };
  150. }
  151. }
  152. if (module.exports) {
  153. module.exports = dataURLtoBlob;
  154. } else {
  155. window.dataURLtoBlob = dataURLtoBlob;
  156. }
  157. })(window);
  158. });
  159. var isBlob = function isBlob(input) {
  160. if (typeof Blob === 'undefined') {
  161. return false;
  162. }
  163. return input instanceof Blob || Object.prototype.toString.call(input) === '[object Blob]';
  164. };
  165. var DEFAULTS = {
  166. /**
  167. * Indicates if output the original image instead of the compressed one
  168. * when the size of the compressed image is greater than the original one's
  169. * @type {boolean}
  170. */
  171. strict: true,
  172. /**
  173. * Indicates if read the image's Exif Orientation information,
  174. * and then rotate or flip the image automatically.
  175. * @type {boolean}
  176. */
  177. checkOrientation: true,
  178. /**
  179. * The max width of the output image.
  180. * @type {number}
  181. */
  182. maxWidth: Infinity,
  183. /**
  184. * The max height of the output image.
  185. * @type {number}
  186. */
  187. maxHeight: Infinity,
  188. /**
  189. * The min width of the output image.
  190. * @type {number}
  191. */
  192. minWidth: 0,
  193. /**
  194. * The min height of the output image.
  195. * @type {number}
  196. */
  197. minHeight: 0,
  198. /**
  199. * The width of the output image.
  200. * If not specified, the natural width of the source image will be used.
  201. * @type {number}
  202. */
  203. width: undefined,
  204. /**
  205. * The height of the output image.
  206. * If not specified, the natural height of the source image will be used.
  207. * @type {number}
  208. */
  209. height: undefined,
  210. /**
  211. * The quality of the output image.
  212. * It must be a number between `0` and `1`,
  213. * and only available for `image/jpeg` and `image/webp` images.
  214. * Check out {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob canvas.toBlob}.
  215. * @type {number}
  216. */
  217. quality: 0.8,
  218. /**
  219. * The mime type of the output image.
  220. * By default, the original mime type of the source image file will be used.
  221. * @type {string}
  222. */
  223. mimeType: 'auto',
  224. /**
  225. * PNG files over this value (5 MB by default) will be converted to JPEGs.
  226. * To disable this, just set the value to `Infinity`.
  227. * @type {number}
  228. */
  229. convertSize: 5000000,
  230. /**
  231. * The hook function to execute before draw the image into the canvas for compression.
  232. * @type {Function}
  233. * @param {CanvasRenderingContext2D} context - The 2d rendering context of the canvas.
  234. * @param {HTMLCanvasElement} canvas - The canvas for compression.
  235. * @example
  236. * function (context, canvas) {
  237. * context.fillStyle = '#fff';
  238. * }
  239. */
  240. beforeDraw: null,
  241. /**
  242. * The hook function to execute after drew the image into the canvas for compression.
  243. * @type {Function}
  244. * @param {CanvasRenderingContext2D} context - The 2d rendering context of the canvas.
  245. * @param {HTMLCanvasElement} canvas - The canvas for compression.
  246. * @example
  247. * function (context, canvas) {
  248. * context.filter = 'grayscale(100%)';
  249. * }
  250. */
  251. drew: null,
  252. /**
  253. * The hook function to execute when success to compress the image.
  254. * @type {Function}
  255. * @param {File} file - The compressed image File object.
  256. * @example
  257. * function (file) {
  258. * console.log(file);
  259. * }
  260. */
  261. success: null,
  262. /**
  263. * The hook function to execute when fail to compress the image.
  264. * @type {Function}
  265. * @param {Error} err - An Error object.
  266. * @example
  267. * function (err) {
  268. * console.log(err.message);
  269. * }
  270. */
  271. error: null
  272. };
  273. var IN_BROWSER = typeof window !== 'undefined';
  274. var WINDOW = IN_BROWSER ? window : {};
  275. var slice = Array.prototype.slice;
  276. /**
  277. * Convert array-like or iterable object to an array.
  278. * @param {*} value - The value to convert.
  279. * @returns {Array} Returns a new array.
  280. */
  281. function toArray(value) {
  282. return Array.from ? Array.from(value) : slice.call(value);
  283. }
  284. var REGEXP_IMAGE_TYPE = /^image\/.+$/;
  285. /**
  286. * Check if the given value is a mime type of image.
  287. * @param {*} value - The value to check.
  288. * @returns {boolean} Returns `true` if the given is a mime type of image, else `false`.
  289. */
  290. function isImageType(value) {
  291. return REGEXP_IMAGE_TYPE.test(value);
  292. }
  293. /**
  294. * Convert image type to extension.
  295. * @param {string} value - The image type to convert.
  296. * @returns {boolean} Returns the image extension.
  297. */
  298. function imageTypeToExtension(value) {
  299. var extension = isImageType(value) ? value.substr(6) : '';
  300. if (extension === 'jpeg') {
  301. extension = 'jpg';
  302. }
  303. return ".".concat(extension);
  304. }
  305. var fromCharCode = String.fromCharCode;
  306. /**
  307. * Get string from char code in data view.
  308. * @param {DataView} dataView - The data view for read.
  309. * @param {number} start - The start index.
  310. * @param {number} length - The read length.
  311. * @returns {string} The read result.
  312. */
  313. function getStringFromCharCode(dataView, start, length) {
  314. var str = '';
  315. var i;
  316. length += start;
  317. for (i = start; i < length; i += 1) {
  318. str += fromCharCode(dataView.getUint8(i));
  319. }
  320. return str;
  321. }
  322. var btoa = WINDOW.btoa;
  323. /**
  324. * Transform array buffer to Data URL.
  325. * @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
  326. * @param {string} mimeType - The mime type of the Data URL.
  327. * @returns {string} The result Data URL.
  328. */
  329. function arrayBufferToDataURL(arrayBuffer, mimeType) {
  330. var chunks = [];
  331. var chunkSize = 8192;
  332. var uint8 = new Uint8Array(arrayBuffer);
  333. while (uint8.length > 0) {
  334. // XXX: Babel's `toConsumableArray` helper will throw error in IE or Safari 9
  335. // eslint-disable-next-line prefer-spread
  336. chunks.push(fromCharCode.apply(null, toArray(uint8.subarray(0, chunkSize))));
  337. uint8 = uint8.subarray(chunkSize);
  338. }
  339. return "data:".concat(mimeType, ";base64,").concat(btoa(chunks.join('')));
  340. }
  341. /**
  342. * Get orientation value from given array buffer.
  343. * @param {ArrayBuffer} arrayBuffer - The array buffer to read.
  344. * @returns {number} The read orientation value.
  345. */
  346. function resetAndGetOrientation(arrayBuffer) {
  347. var dataView = new DataView(arrayBuffer);
  348. var orientation; // Ignores range error when the image does not have correct Exif information
  349. try {
  350. var littleEndian;
  351. var app1Start;
  352. var ifdStart; // Only handle JPEG image (start by 0xFFD8)
  353. if (dataView.getUint8(0) === 0xFF && dataView.getUint8(1) === 0xD8) {
  354. var length = dataView.byteLength;
  355. var offset = 2;
  356. while (offset + 1 < length) {
  357. if (dataView.getUint8(offset) === 0xFF && dataView.getUint8(offset + 1) === 0xE1) {
  358. app1Start = offset;
  359. break;
  360. }
  361. offset += 1;
  362. }
  363. }
  364. if (app1Start) {
  365. var exifIDCode = app1Start + 4;
  366. var tiffOffset = app1Start + 10;
  367. if (getStringFromCharCode(dataView, exifIDCode, 4) === 'Exif') {
  368. var endianness = dataView.getUint16(tiffOffset);
  369. littleEndian = endianness === 0x4949;
  370. if (littleEndian || endianness === 0x4D4D
  371. /* bigEndian */
  372. ) {
  373. if (dataView.getUint16(tiffOffset + 2, littleEndian) === 0x002A) {
  374. var firstIFDOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
  375. if (firstIFDOffset >= 0x00000008) {
  376. ifdStart = tiffOffset + firstIFDOffset;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. if (ifdStart) {
  383. var _length = dataView.getUint16(ifdStart, littleEndian);
  384. var _offset;
  385. var i;
  386. for (i = 0; i < _length; i += 1) {
  387. _offset = ifdStart + i * 12 + 2;
  388. if (dataView.getUint16(_offset, littleEndian) === 0x0112
  389. /* Orientation */
  390. ) {
  391. // 8 is the offset of the current tag's value
  392. _offset += 8; // Get the original orientation value
  393. orientation = dataView.getUint16(_offset, littleEndian); // Override the orientation with its default value
  394. dataView.setUint16(_offset, 1, littleEndian);
  395. break;
  396. }
  397. }
  398. }
  399. } catch (e) {
  400. orientation = 1;
  401. }
  402. return orientation;
  403. }
  404. /**
  405. * Parse Exif Orientation value.
  406. * @param {number} orientation - The orientation to parse.
  407. * @returns {Object} The parsed result.
  408. */
  409. function parseOrientation(orientation) {
  410. var rotate = 0;
  411. var scaleX = 1;
  412. var scaleY = 1;
  413. switch (orientation) {
  414. // Flip horizontal
  415. case 2:
  416. scaleX = -1;
  417. break;
  418. // Rotate left 180°
  419. case 3:
  420. rotate = -180;
  421. break;
  422. // Flip vertical
  423. case 4:
  424. scaleY = -1;
  425. break;
  426. // Flip vertical and rotate right 90°
  427. case 5:
  428. rotate = 90;
  429. scaleY = -1;
  430. break;
  431. // Rotate right 90°
  432. case 6:
  433. rotate = 90;
  434. break;
  435. // Flip horizontal and rotate right 90°
  436. case 7:
  437. rotate = 90;
  438. scaleX = -1;
  439. break;
  440. // Rotate left 90°
  441. case 8:
  442. rotate = -90;
  443. break;
  444. default:
  445. }
  446. return {
  447. rotate: rotate,
  448. scaleX: scaleX,
  449. scaleY: scaleY
  450. };
  451. }
  452. var REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/;
  453. /**
  454. * Normalize decimal number.
  455. * Check out {@link http://0.30000000000000004.com/}
  456. * @param {number} value - The value to normalize.
  457. * @param {number} [times=100000000000] - The times for normalizing.
  458. * @returns {number} Returns the normalized number.
  459. */
  460. function normalizeDecimalNumber(value) {
  461. var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100000000000;
  462. return REGEXP_DECIMALS.test(value) ? Math.round(value * times) / times : value;
  463. }
  464. var ArrayBuffer$1 = WINDOW.ArrayBuffer,
  465. FileReader = WINDOW.FileReader;
  466. var URL = WINDOW.URL || WINDOW.webkitURL;
  467. var REGEXP_EXTENSION = /\.\w+$/;
  468. var AnotherCompressor = WINDOW.Compressor;
  469. /**
  470. * Creates a new image compressor.
  471. * @class
  472. */
  473. var Compressor =
  474. /*#__PURE__*/
  475. function () {
  476. /**
  477. * The constructor of Compressor.
  478. * @param {File|Blob} file - The target image file for compressing.
  479. * @param {Object} [options] - The options for compressing.
  480. */
  481. function Compressor(file, options) {
  482. _classCallCheck(this, Compressor);
  483. this.file = file;
  484. this.image = new Image();
  485. this.options = _objectSpread({}, DEFAULTS, options);
  486. this.aborted = false;
  487. this.result = null;
  488. this.init();
  489. }
  490. _createClass(Compressor, [{
  491. key: "init",
  492. value: function init() {
  493. var _this = this;
  494. var file = this.file,
  495. options = this.options;
  496. if (!isBlob(file)) {
  497. this.fail(new Error('The first argument must be a File or Blob object.'));
  498. return;
  499. }
  500. var mimeType = file.type;
  501. if (!isImageType(mimeType)) {
  502. this.fail(new Error('The first argument must be an image File or Blob object.'));
  503. return;
  504. }
  505. if (!URL || !FileReader) {
  506. this.fail(new Error('The current browser does not support image compression.'));
  507. return;
  508. }
  509. if (!ArrayBuffer$1) {
  510. options.checkOrientation = false;
  511. }
  512. if (URL && !options.checkOrientation) {
  513. this.load({
  514. url: URL.createObjectURL(file)
  515. });
  516. } else {
  517. var reader = new FileReader();
  518. var checkOrientation = options.checkOrientation && mimeType === 'image/jpeg';
  519. this.reader = reader;
  520. reader.onload = function (_ref) {
  521. var target = _ref.target;
  522. var result = target.result;
  523. var data = {};
  524. if (checkOrientation) {
  525. // Reset the orientation value to its default value 1
  526. // as some iOS browsers will render image with its orientation
  527. var orientation = resetAndGetOrientation(result);
  528. if (orientation > 1 || !URL) {
  529. // Generate a new URL which has the default orientation value
  530. data.url = arrayBufferToDataURL(result, mimeType);
  531. if (orientation > 1) {
  532. _extends(data, parseOrientation(orientation));
  533. }
  534. } else {
  535. data.url = URL.createObjectURL(file);
  536. }
  537. } else {
  538. data.url = result;
  539. }
  540. _this.load(data);
  541. };
  542. reader.onabort = function () {
  543. _this.fail(new Error('Aborted to read the image with FileReader.'));
  544. };
  545. reader.onerror = function () {
  546. _this.fail(new Error('Failed to read the image with FileReader.'));
  547. };
  548. reader.onloadend = function () {
  549. _this.reader = null;
  550. };
  551. if (checkOrientation) {
  552. reader.readAsArrayBuffer(file);
  553. } else {
  554. reader.readAsDataURL(file);
  555. }
  556. }
  557. }
  558. }, {
  559. key: "load",
  560. value: function load(data) {
  561. var _this2 = this;
  562. var file = this.file,
  563. image = this.image;
  564. image.onload = function () {
  565. _this2.draw(_objectSpread({}, data, {
  566. naturalWidth: image.naturalWidth,
  567. naturalHeight: image.naturalHeight
  568. }));
  569. };
  570. image.onabort = function () {
  571. _this2.fail(new Error('Aborted to load the image.'));
  572. };
  573. image.onerror = function () {
  574. _this2.fail(new Error('Failed to load the image.'));
  575. };
  576. image.alt = file.name;
  577. image.src = data.url;
  578. }
  579. }, {
  580. key: "draw",
  581. value: function draw(_ref2) {
  582. var _this3 = this;
  583. var naturalWidth = _ref2.naturalWidth,
  584. naturalHeight = _ref2.naturalHeight,
  585. _ref2$rotate = _ref2.rotate,
  586. rotate = _ref2$rotate === void 0 ? 0 : _ref2$rotate,
  587. _ref2$scaleX = _ref2.scaleX,
  588. scaleX = _ref2$scaleX === void 0 ? 1 : _ref2$scaleX,
  589. _ref2$scaleY = _ref2.scaleY,
  590. scaleY = _ref2$scaleY === void 0 ? 1 : _ref2$scaleY;
  591. var file = this.file,
  592. image = this.image,
  593. options = this.options;
  594. var canvas = document.createElement('canvas');
  595. var context = canvas.getContext('2d');
  596. var aspectRatio = naturalWidth / naturalHeight;
  597. var is90DegreesRotated = Math.abs(rotate) % 180 === 90;
  598. var maxWidth = Math.max(options.maxWidth, 0) || Infinity;
  599. var maxHeight = Math.max(options.maxHeight, 0) || Infinity;
  600. var minWidth = Math.max(options.minWidth, 0) || 0;
  601. var minHeight = Math.max(options.minHeight, 0) || 0;
  602. var width = Math.max(options.width, 0) || naturalWidth;
  603. var height = Math.max(options.height, 0) || naturalHeight;
  604. if (is90DegreesRotated) {
  605. var _ref3 = [maxHeight, maxWidth];
  606. maxWidth = _ref3[0];
  607. maxHeight = _ref3[1];
  608. var _ref4 = [minHeight, minWidth];
  609. minWidth = _ref4[0];
  610. minHeight = _ref4[1];
  611. var _ref5 = [height, width];
  612. width = _ref5[0];
  613. height = _ref5[1];
  614. }
  615. if (maxWidth < Infinity && maxHeight < Infinity) {
  616. if (maxHeight * aspectRatio > maxWidth) {
  617. maxHeight = maxWidth / aspectRatio;
  618. } else {
  619. maxWidth = maxHeight * aspectRatio;
  620. }
  621. } else if (maxWidth < Infinity) {
  622. maxHeight = maxWidth / aspectRatio;
  623. } else if (maxHeight < Infinity) {
  624. maxWidth = maxHeight * aspectRatio;
  625. }
  626. if (minWidth > 0 && minHeight > 0) {
  627. if (minHeight * aspectRatio > minWidth) {
  628. minHeight = minWidth / aspectRatio;
  629. } else {
  630. minWidth = minHeight * aspectRatio;
  631. }
  632. } else if (minWidth > 0) {
  633. minHeight = minWidth / aspectRatio;
  634. } else if (minHeight > 0) {
  635. minWidth = minHeight * aspectRatio;
  636. }
  637. if (height * aspectRatio > width) {
  638. height = width / aspectRatio;
  639. } else {
  640. width = height * aspectRatio;
  641. }
  642. width = Math.floor(normalizeDecimalNumber(Math.min(Math.max(width, minWidth), maxWidth)));
  643. height = Math.floor(normalizeDecimalNumber(Math.min(Math.max(height, minHeight), maxHeight)));
  644. var destX = -width / 2;
  645. var destY = -height / 2;
  646. var destWidth = width;
  647. var destHeight = height;
  648. if (is90DegreesRotated) {
  649. var _ref6 = [height, width];
  650. width = _ref6[0];
  651. height = _ref6[1];
  652. }
  653. canvas.width = width;
  654. canvas.height = height;
  655. if (!isImageType(options.mimeType)) {
  656. options.mimeType = file.type;
  657. }
  658. var fillStyle = 'transparent'; // Converts PNG files over the `convertSize` to JPEGs.
  659. if (file.size > options.convertSize && options.mimeType === 'image/png') {
  660. fillStyle = '#fff';
  661. options.mimeType = 'image/jpeg';
  662. } // Override the default fill color (#000, black)
  663. context.fillStyle = fillStyle;
  664. context.fillRect(0, 0, width, height);
  665. if (options.beforeDraw) {
  666. options.beforeDraw.call(this, context, canvas);
  667. }
  668. if (this.aborted) {
  669. return;
  670. }
  671. context.save();
  672. context.translate(width / 2, height / 2);
  673. context.rotate(rotate * Math.PI / 180);
  674. context.scale(scaleX, scaleY);
  675. context.drawImage(image, destX, destY, destWidth, destHeight);
  676. context.restore();
  677. if (options.drew) {
  678. options.drew.call(this, context, canvas);
  679. }
  680. if (this.aborted) {
  681. return;
  682. }
  683. var done = function done(result) {
  684. if (!_this3.aborted) {
  685. _this3.done({
  686. naturalWidth: naturalWidth,
  687. naturalHeight: naturalHeight,
  688. result: result
  689. });
  690. }
  691. };
  692. if (canvas.toBlob) {
  693. canvas.toBlob(done, options.mimeType, options.quality);
  694. } else {
  695. done(canvasToBlob(canvas.toDataURL(options.mimeType, options.quality)));
  696. }
  697. }
  698. }, {
  699. key: "done",
  700. value: function done(_ref7) {
  701. var naturalWidth = _ref7.naturalWidth,
  702. naturalHeight = _ref7.naturalHeight,
  703. result = _ref7.result;
  704. var file = this.file,
  705. image = this.image,
  706. options = this.options;
  707. if (URL && !options.checkOrientation) {
  708. URL.revokeObjectURL(image.src);
  709. }
  710. if (result) {
  711. // Returns original file if the result is greater than it and without size related options
  712. if (options.strict && result.size > file.size && options.mimeType === file.type && !(options.width > naturalWidth || options.height > naturalHeight || options.minWidth > naturalWidth || options.minHeight > naturalHeight)) {
  713. result = file;
  714. } else {
  715. var date = new Date();
  716. result.lastModified = date.getTime();
  717. result.lastModifiedDate = date;
  718. result.name = file.name; // Convert the extension to match its type
  719. if (result.name && result.type !== file.type) {
  720. result.name = result.name.replace(REGEXP_EXTENSION, imageTypeToExtension(result.type));
  721. }
  722. }
  723. } else {
  724. // Returns original file if the result is null in some cases.
  725. result = file;
  726. }
  727. this.result = result;
  728. if (options.success) {
  729. options.success.call(this, result);
  730. }
  731. }
  732. }, {
  733. key: "fail",
  734. value: function fail(err) {
  735. var options = this.options;
  736. if (options.error) {
  737. options.error.call(this, err);
  738. } else {
  739. throw err;
  740. }
  741. }
  742. }, {
  743. key: "abort",
  744. value: function abort() {
  745. if (!this.aborted) {
  746. this.aborted = true;
  747. if (this.reader) {
  748. this.reader.abort();
  749. } else if (!this.image.complete) {
  750. this.image.onload = null;
  751. this.image.onabort();
  752. } else {
  753. this.fail(new Error('The compression process has been aborted.'));
  754. }
  755. }
  756. }
  757. /**
  758. * Get the no conflict compressor class.
  759. * @returns {Compressor} The compressor class.
  760. */
  761. }], [{
  762. key: "noConflict",
  763. value: function noConflict() {
  764. window.Compressor = AnotherCompressor;
  765. return Compressor;
  766. }
  767. /**
  768. * Change the default options.
  769. * @param {Object} options - The new default options.
  770. */
  771. }, {
  772. key: "setDefaults",
  773. value: function setDefaults(options) {
  774. _extends(DEFAULTS, options);
  775. }
  776. }]);
  777. return Compressor;
  778. }();
  779. return Compressor;
  780. }));