imageConversion.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ! function(t, e) {
  2. "object" == typeof exports && "object" == typeof module ? module.exports = e() : "function" == typeof define &&
  3. define.amd ? define([], e) : "object" == typeof exports ? exports.imageConversion = e() : t.imageConversion =
  4. e()
  5. }(this, (function() {
  6. return function(t) {
  7. var e = {};
  8. function n(r) {
  9. if (e[r]) return e[r].exports;
  10. var o = e[r] = {
  11. i: r,
  12. l: !1,
  13. exports: {}
  14. };
  15. return t[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports
  16. }
  17. return n.m = t, n.c = e, n.d = function(t, e, r) {
  18. n.o(t, e) || Object.defineProperty(t, e, {
  19. enumerable: !0,
  20. get: r
  21. })
  22. }, n.r = function(t) {
  23. "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol
  24. .toStringTag, {
  25. value: "Module"
  26. }), Object.defineProperty(t, "__esModule", {
  27. value: !0
  28. })
  29. }, n.t = function(t, e) {
  30. if (1 & e && (t = n(t)), 8 & e) return t;
  31. if (4 & e && "object" == typeof t && t && t.__esModule) return t;
  32. var r = Object.create(null);
  33. if (n.r(r), Object.defineProperty(r, "default", {
  34. enumerable: !0,
  35. value: t
  36. }), 2 & e && "string" != typeof t)
  37. for (var o in t) n.d(r, o, function(e) {
  38. return t[e]
  39. }.bind(null, o));
  40. return r
  41. }, n.n = function(t) {
  42. var e = t && t.__esModule ? function() {
  43. return t.default
  44. } : function() {
  45. return t
  46. };
  47. return n.d(e, "a", e), e
  48. }, n.o = function(t, e) {
  49. return Object.prototype.hasOwnProperty.call(t, e)
  50. }, n.p = "", n(n.s = 0)
  51. }([function(t, e, n) {
  52. "use strict";
  53. var r;
  54. function o(t) {
  55. return ["image/png", "image/jpeg", "image/gif"].some(e => e === t)
  56. }
  57. n.r(e), n.d(e, "canvastoDataURL", (function() {
  58. return a
  59. })), n.d(e, "canvastoFile", (function() {
  60. return c
  61. })), n.d(e, "dataURLtoFile", (function() {
  62. return s
  63. })), n.d(e, "dataURLtoImage", (function() {
  64. return l
  65. })), n.d(e, "downloadFile", (function() {
  66. return d
  67. })), n.d(e, "filetoDataURL", (function() {
  68. return f
  69. })), n.d(e, "imagetoCanvas", (function() {
  70. return g
  71. })), n.d(e, "urltoBlob", (function() {
  72. return w
  73. })), n.d(e, "urltoImage", (function() {
  74. return m
  75. })), n.d(e, "compress", (function() {
  76. return p
  77. })), n.d(e, "compressAccurately", (function() {
  78. return b
  79. })), n.d(e, "EImageType", (function() {
  80. return r
  81. })),
  82. function(t) {
  83. t.PNG = "image/png", t.JPEG = "image/jpeg", t.GIF = "image/gif"
  84. }(r || (r = {}));
  85. var i = function(t, e, n, r) {
  86. return new(n || (n = Promise))((function(o, i) {
  87. function a(t) {
  88. try {
  89. u(r.next(t))
  90. } catch (t) {
  91. i(t)
  92. }
  93. }
  94. function c(t) {
  95. try {
  96. u(r.throw(t))
  97. } catch (t) {
  98. i(t)
  99. }
  100. }
  101. function u(t) {
  102. var e;
  103. t.done ? o(t.value) : (e = t.value, e instanceof n ? e : new n((
  104. function(t) {
  105. t(e)
  106. }))).then(a, c)
  107. }
  108. u((r = r.apply(t, e || [])).next())
  109. }))
  110. };
  111. function a(t, e = .92, n = r.JPEG) {
  112. return i(this, void 0, void 0, (function*() {
  113. return o(n) || (n = r.JPEG), t.toDataURL(n, e)
  114. }))
  115. }
  116. function c(t, e = .92, n = r.JPEG) {
  117. return new Promise(r => t.toBlob(t => r(t), n, e))
  118. }
  119. var u = function(t, e, n, r) {
  120. return new(n || (n = Promise))((function(o, i) {
  121. function a(t) {
  122. try {
  123. u(r.next(t))
  124. } catch (t) {
  125. i(t)
  126. }
  127. }
  128. function c(t) {
  129. try {
  130. u(r.throw(t))
  131. } catch (t) {
  132. i(t)
  133. }
  134. }
  135. function u(t) {
  136. var e;
  137. t.done ? o(t.value) : (e = t.value, e instanceof n ? e : new n((
  138. function(t) {
  139. t(e)
  140. }))).then(a, c)
  141. }
  142. u((r = r.apply(t, e || [])).next())
  143. }))
  144. };
  145. function s(t, e) {
  146. return u(this, void 0, void 0, (function*() {
  147. const n = t.split(",");
  148. let r = n[0].match(/:(.*?);/)[1];
  149. const i = atob(n[1]);
  150. let a = i.length;
  151. const c = new Uint8Array(a);
  152. for (; a--;) c[a] = i.charCodeAt(a);
  153. return o(e) && (r = e), new Blob([c], {
  154. type: r
  155. })
  156. }))
  157. }
  158. function l(t) {
  159. return new Promise((e, n) => {
  160. const r = new Image;
  161. r.onload = () => e(r), r.onerror = () => n(new Error(
  162. "dataURLtoImage(): dataURL is illegal")), r.src = t
  163. })
  164. }
  165. function d(t, e) {
  166. const n = document.createElement("a");
  167. n.href = window.URL.createObjectURL(t), n.download = e || Date.now().toString(36),
  168. document.body.appendChild(n);
  169. const r = document.createEvent("MouseEvents");
  170. r.initEvent("click", !1, !1), n.dispatchEvent(r), document.body.removeChild(n)
  171. }
  172. function f(t) {
  173. return new Promise(e => {
  174. const n = new FileReader;
  175. n.onloadend = t => e(t.target.result), n.readAsDataURL(t)
  176. })
  177. }
  178. var h = function(t, e, n, r) {
  179. return new(n || (n = Promise))((function(o, i) {
  180. function a(t) {
  181. try {
  182. u(r.next(t))
  183. } catch (t) {
  184. i(t)
  185. }
  186. }
  187. function c(t) {
  188. try {
  189. u(r.throw(t))
  190. } catch (t) {
  191. i(t)
  192. }
  193. }
  194. function u(t) {
  195. var e;
  196. t.done ? o(t.value) : (e = t.value, e instanceof n ? e : new n((
  197. function(t) {
  198. t(e)
  199. }))).then(a, c)
  200. }
  201. u((r = r.apply(t, e || [])).next())
  202. }))
  203. };
  204. function g(t, e = {}) {
  205. return h(this, void 0, void 0, (function*() {
  206. const n = Object.assign({}, e),
  207. r = document.createElement("canvas"),
  208. o = r.getContext("2d");
  209. let i, a;
  210. for (const t in n) Object.prototype.hasOwnProperty.call(n, t) && (n[t] =
  211. Number(n[t]));
  212. if (n.scale) {
  213. const e = n.scale > 0 && n.scale < 10 ? n.scale : 1;
  214. a = t.width * e, i = t.height * e
  215. } else a = n.width || n.height * t.width / t.height || t.width, i = n
  216. .height || n.width * t.height / t.width || t.height;
  217. switch ([5, 6, 7, 8].some(t => t === n.orientation) ? (r.height = a, r
  218. .width = i) : (r.height = i, r.width = a), n.orientation) {
  219. case 3:
  220. o.rotate(180 * Math.PI / 180), o.drawImage(t, -r.width, -r
  221. .height, r.width, r.height);
  222. break;
  223. case 6:
  224. o.rotate(90 * Math.PI / 180), o.drawImage(t, 0, -r.width, r
  225. .height, r.width);
  226. break;
  227. case 8:
  228. o.rotate(270 * Math.PI / 180), o.drawImage(t, -r.height, 0, r
  229. .height, r.width);
  230. break;
  231. case 2:
  232. o.translate(r.width, 0), o.scale(-1, 1), o.drawImage(t, 0, 0, r
  233. .width, r.height);
  234. break;
  235. case 4:
  236. o.translate(r.width, 0), o.scale(-1, 1), o.rotate(180 * Math
  237. .PI / 180), o.drawImage(t, -r.width, -r.height, r.width,
  238. r.height);
  239. break;
  240. case 5:
  241. o.translate(r.width, 0), o.scale(-1, 1), o.rotate(90 * Math.PI /
  242. 180), o.drawImage(t, 0, -r.width, r.height, r.width);
  243. break;
  244. case 7:
  245. o.translate(r.width, 0), o.scale(-1, 1), o.rotate(270 * Math
  246. .PI / 180), o.drawImage(t, -r.height, 0, r.height, r
  247. .width);
  248. break;
  249. default:
  250. o.drawImage(t, 0, 0, r.width, r.height)
  251. }
  252. return r
  253. }))
  254. }
  255. function w(t) {
  256. return fetch(t).then(t => t.blob())
  257. }
  258. function m(t) {
  259. return new Promise((e, n) => {
  260. const r = new Image;
  261. r.onload = () => e(r), r.onerror = () => n(new Error(
  262. "urltoImage(): Image failed to load, please check the image URL"
  263. )), r.src = t
  264. })
  265. }
  266. var y = function(t, e, n, r) {
  267. return new(n || (n = Promise))((function(o, i) {
  268. function a(t) {
  269. try {
  270. u(r.next(t))
  271. } catch (t) {
  272. i(t)
  273. }
  274. }
  275. function c(t) {
  276. try {
  277. u(r.throw(t))
  278. } catch (t) {
  279. i(t)
  280. }
  281. }
  282. function u(t) {
  283. var e;
  284. t.done ? o(t.value) : (e = t.value, e instanceof n ? e : new n((
  285. function(t) {
  286. t(e)
  287. }))).then(a, c)
  288. }
  289. u((r = r.apply(t, e || [])).next())
  290. }))
  291. };
  292. function p(t, e = {}) {
  293. return y(this, void 0, void 0, (function*() {
  294. if (!(t instanceof Blob)) throw new Error(
  295. "compress(): First arg must be a Blob object or a File object."
  296. );
  297. if ("object" != typeof e && (e = Object.assign({
  298. quality: e
  299. })), e.quality = Number(e.quality), Number.isNaN(e.quality))
  300. return t;
  301. const n = yield f(t);
  302. let i = n.split(",")[0].match(/:(.*?);/)[1],
  303. c = r.JPEG;
  304. o(e.type) && (c = e.type, i = e.type);
  305. const u = yield l(n), d = yield g(u, Object.assign({}, e)), h = yield a(
  306. d, e.quality, c), w = yield s(h, i);
  307. return w.size > t.size ? t : w
  308. }))
  309. }
  310. function b(t, e = {}) {
  311. return y(this, void 0, void 0, (function*() {
  312. if (!(t instanceof Blob)) throw new Error(
  313. "compressAccurately(): First arg must be a Blob object or a File object."
  314. );
  315. if ("object" != typeof e && (e = Object.assign({
  316. size: e
  317. })), e.size = Number(e.size), Number.isNaN(e.size)) return t;
  318. if (1024 * e.size > t.size) return t;
  319. e.accuracy = Number(e.accuracy), (!e.accuracy || e.accuracy < .8 || e
  320. .accuracy > .99) && (e.accuracy = .95);
  321. const n = e.size * (2 - e.accuracy) * 1024,
  322. i = 1024 * e.size,
  323. c = e.size * e.accuracy * 1024,
  324. u = yield f(t);
  325. let d = u.split(",")[0].match(/:(.*?);/)[1],
  326. h = r.JPEG;
  327. o(e.type) && (h = e.type, d = e.type);
  328. const w = yield l(u), m = yield g(w, Object.assign({}, e));
  329. let y, p = .5;
  330. const b = [null, null];
  331. for (let t = 1; t <= 7; t++) {
  332. y = yield a(m, p, h);
  333. const e = .75 * y.length;
  334. if (7 === t) {
  335. (n < e || c > e) && (y = [y, ...b].filter(t => t).sort((t, e) =>
  336. Math.abs(.75 * t.length - i) - Math.abs(.75 * e.length -
  337. i))[0]);
  338. break
  339. }
  340. if (n < e) b[1] = y, p -= Math.pow(.5, t + 1);
  341. else {
  342. if (!(c > e)) break;
  343. b[0] = y, p += Math.pow(.5, t + 1)
  344. }
  345. }
  346. const v = yield s(y, d);
  347. return v.size > t.size ? t : v
  348. }))
  349. }
  350. }])
  351. }));