layui-vue/es/_chunks/js-image-compressor/index.js

262 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2024-10-16 07:24:01 +00:00
var jsImageCompressor = { exports: {} };
var imageCompressor_min = { exports: {} };
(function(module, exports) {
!function(e, t) {
module.exports = t();
}(window, function() {
return function(e) {
var t = {};
function n(r) {
if (t[r])
return t[r].exports;
var a = t[r] = { i: r, l: false, exports: {} };
return e[r].call(a.exports, a, a.exports, n), a.l = true, a.exports;
}
return n.m = e, n.c = t, n.d = function(e2, t2, r) {
n.o(e2, t2) || Object.defineProperty(e2, t2, { enumerable: true, get: r });
}, n.r = function(e2) {
"undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e2, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e2, "__esModule", { value: true });
}, n.t = function(e2, t2) {
if (1 & t2 && (e2 = n(e2)), 8 & t2)
return e2;
if (4 & t2 && "object" == typeof e2 && e2 && e2.__esModule)
return e2;
var r = /* @__PURE__ */ Object.create(null);
if (n.r(r), Object.defineProperty(r, "default", { enumerable: true, value: e2 }), 2 & t2 && "string" != typeof e2)
for (var a in e2)
n.d(r, a, function(t3) {
return e2[t3];
}.bind(null, a));
return r;
}, n.n = function(e2) {
var t2 = e2 && e2.__esModule ? function() {
return e2.default;
} : function() {
return e2;
};
return n.d(t2, "a", t2), t2;
}, n.o = function(e2, t2) {
return Object.prototype.hasOwnProperty.call(e2, t2);
}, n.p = "", n(n.s = 0);
}([function(e, t, n) {
n.r(t);
var r = window, a = /^image\//, i = /\.\w+$/, o = {}, s = { file: null, quality: 0.8, convertSize: 2048e3, loose: true, redressOrientation: true }, f = function(e2) {
return "function" == typeof e2;
}, c = function(e2) {
return a.test(e2);
};
function l(e2) {
e2 = Object.assign({}, s, e2), this.options = e2, this.file = e2.file, this.image = null, this.ParsedOrientationInfo = null, this.init();
}
var u = l.prototype;
for (var h in t.default = l, u.init = function() {
var e2 = this, t2 = this.file, n2 = this.options;
t2 && c(t2.type) ? (c(n2.mimeType) || (n2.mimeType = t2.type), o.file2Image(t2, function(r2) {
f(e2.beforeCompress) && (e2.image = r2, t2.width = r2.naturalWidth, t2.height = r2.naturalHeight, e2.beforeCompress(t2)), "image/jpeg" === t2.type && n2.redressOrientation ? e2.getParsedOrientationInfo(function(t3) {
e2.parsedOrientationInfo = t3, e2.rendCanvas();
}) : (e2.parsedOrientationInfo = { rotate: 0, scaleX: 1, scaleY: 1 }, e2.rendCanvas());
}, e2.error)) : e2.error("\u8BF7\u4E0A\u4F20\u56FE\u7247\u6587\u4EF6!");
}, u.rendCanvas = function() {
var e2 = this, t2 = this.options, n2 = this.image, r2 = this.getExpectedEdge(), a2 = r2.dWidth, i2 = r2.dHeight, s2 = r2.width, f2 = r2.height, c2 = o.image2Canvas(n2, a2, i2, e2.beforeDraw.bind(e2), e2.afterDraw.bind(e2), s2, f2);
o.canvas2Blob(c2, function(t3) {
t3 && (t3.width = c2.width, t3.height = c2.height), e2.success(t3);
}, t2.quality, t2.mimeType);
}, u.beforeCompress = function() {
f(this.options.beforeCompress) && this.options.beforeCompress(this.file);
}, u.getExpectedEdge = function() {
var e2, t2 = this.image, n2 = this.parsedOrientationInfo.rotate, r2 = this.options, a2 = t2.naturalWidth, i2 = t2.naturalHeight, o2 = Math.abs(n2) % 180 == 90;
o2 && (e2 = i2, i2 = a2, a2 = e2);
var s2 = a2 / i2, f2 = Math.max(r2.maxWidth, 0) || 1 / 0, c2 = Math.max(r2.maxHeight, 0) || 1 / 0, l2 = Math.max(r2.minWidth, 0) || 0, u2 = Math.max(r2.minHeight, 0) || 0, h2 = Math.max(r2.width, 0) || a2, d = Math.max(r2.height, 0) || i2;
f2 < 1 / 0 && c2 < 1 / 0 ? c2 * s2 > f2 ? c2 = f2 / s2 : f2 = c2 * s2 : f2 < 1 / 0 ? c2 = f2 / s2 : c2 < 1 / 0 && (f2 = c2 * s2), l2 > 0 && u2 > 0 ? u2 * s2 > l2 ? u2 = l2 / s2 : l2 = u2 * s2 : l2 > 0 ? u2 = l2 / s2 : u2 > 0 && (l2 = u2 * s2), d * s2 > h2 ? d = h2 / s2 : h2 = d * s2;
var g = h2 = Math.floor(Math.min(Math.max(h2, l2), f2)), p = d = Math.floor(Math.min(Math.max(d, u2), c2));
return o2 && (e2 = p, p = g, g = e2), { dWidth: g, dHeight: p, width: h2, height: d };
}, u.getParsedOrientationInfo = function(e2) {
var t2 = this;
this.getOrientation(function(n2) {
f(e2) && e2(t2.parseOrientation(n2));
});
}, u.getOrientation = function(e2) {
var t2 = this;
o.file2ArrayBuffer(this.file, function(n2) {
f(e2) && e2(t2.resetAndGetOrientation(n2));
});
}, u.resetAndGetOrientation = function(e2) {
var t2, n2 = new DataView(e2);
try {
var r2, a2, i2;
if (255 === n2.getUint8(0) && 216 === n2.getUint8(1))
for (var s2 = n2.byteLength, f2 = 2; f2 + 1 < s2; ) {
if (255 === n2.getUint8(f2) && 225 === n2.getUint8(f2 + 1)) {
a2 = f2;
break;
}
f2 += 1;
}
if (a2) {
var c2 = a2 + 4, l2 = a2 + 10;
if ("Exif" === o.getStringFromCharCode(n2, c2, 4)) {
var u2 = n2.getUint16(l2);
if (((r2 = 18761 === u2) || 19789 === u2) && 42 === n2.getUint16(l2 + 2, r2)) {
var h2 = n2.getUint32(l2 + 4, r2);
h2 >= 8 && (i2 = l2 + h2);
}
}
}
if (i2) {
var d;
s2 = n2.getUint16(i2, r2);
for (d = 0; d < s2; d += 1)
if (f2 = i2 + 12 * d + 2, 274 === n2.getUint16(f2, r2)) {
f2 += 8, t2 = n2.getUint16(f2, r2), n2.setUint16(f2, 1, r2);
break;
}
}
} catch (e3) {
console.error(e3), t2 = 1;
}
return t2;
}, u.parseOrientation = function(e2) {
var t2 = 0, n2 = 1, r2 = 1;
switch (e2) {
case 2:
n2 = -1;
break;
case 3:
t2 = -180;
break;
case 4:
r2 = -1;
break;
case 5:
t2 = 90, r2 = -1;
break;
case 6:
t2 = 90;
break;
case 7:
t2 = 90, n2 = -1;
break;
case 8:
t2 = -90;
}
return { rotate: t2, scaleX: n2, scaleY: r2 };
}, u.beforeDraw = function(e2, t2) {
var n2 = this.parsedOrientationInfo, r2 = n2.rotate, a2 = n2.scaleX, i2 = n2.scaleY, o2 = this.file, s2 = this.options, c2 = "transparent", l2 = t2.width, u2 = t2.height;
switch (o2.size > s2.convertSize && "image/png" === s2.mimeType && (c2 = "#fff", s2.mimeType = "image/jpeg"), e2.fillStyle = c2, e2.fillRect(0, 0, l2, u2), f(s2.beforeDraw) && s2.beforeDraw.call(this, e2, t2), e2.save(), r2) {
case 90:
e2.translate(l2, 0);
break;
case -90:
e2.translate(0, u2);
break;
case -180:
e2.translate(l2, u2);
}
e2.rotate(r2 * Math.PI / 180), e2.scale(a2, i2);
}, u.afterDraw = function(e2, t2) {
var n2 = this.options;
f(n2.afterDraw) && n2.afterDraw.call(this, e2, t2);
}, u.error = function(e2) {
var t2 = this.options;
if (!f(t2.error))
throw new Error(e2);
t2.error.call(this, e2);
}, u.success = function(e2) {
var t2, n2, r2 = this.options, a2 = this.file, o2 = this.image, s2 = this.getExpectedEdge(), l2 = o2.naturalHeight, u2 = o2.naturalWidth;
if (e2 && e2.size)
if (!r2.loose && e2.size > a2.size && !(s2.width > u2 || s2.height > l2))
console.warn("\u5F53\u524D\u8BBE\u7F6E\u7684\u662F\u975E\u5BBD\u677E\u6A21\u5F0F\uFF0C\u538B\u7F29\u7ED3\u679C\u5927\u4E8E\u6E90\u56FE\u7247\uFF0C\u8F93\u51FA\u6E90\u56FE\u7247"), e2 = a2;
else {
var h2 = new Date();
e2.lastModified = h2.getTime(), e2.lastModifiedDate = h2, e2.name = a2.name, e2.name && e2.type !== a2.type && (e2.name = e2.name.replace(i, (t2 = e2.type, "jpeg" === (n2 = c(t2) ? t2.substr(6) : "") && (n2 = "jpg"), "." + n2)));
}
else
console.warn("\u56FE\u7247\u538B\u7F29\u51FA\u4E86\u70B9\u610F\u5916\uFF0C\u8F93\u51FA\u6E90\u56FE\u7247"), e2 = a2;
f(r2.success) && r2.success.call(this, e2);
}, o.file2DataUrl = function(e2, t2, n2) {
var r2 = new FileReader();
r2.onload = function() {
t2(r2.result);
}, r2.onerror = function() {
f(n2) && n2("\u8BFB\u53D6\u6587\u4EF6\u5931\u8D25\uFF01");
}, r2.readAsDataURL(e2);
}, o.file2ArrayBuffer = function(e2, t2, n2) {
var r2 = new FileReader();
r2.onload = function(e3) {
t2(e3.target.result);
}, r2.onerror = function() {
f(n2) && n2("\u8BFB\u53D6\u6587\u4EF6\u5931\u8D25\uFF01");
}, r2.readAsArrayBuffer(e2);
}, o.getStringFromCharCode = function(e2, t2, n2) {
var r2, a2 = "";
for (n2 += t2, r2 = t2; r2 < n2; r2 += 1)
a2 += String.fromCharCode(e2.getUint8(r2));
return a2;
}, o.file2Image = function(e2, t2, n2) {
var a2 = new Image(), i2 = r.URL || r.webkitURL;
if (r.navigator && /(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(r.navigator.userAgent) && (a2.crossOrigin = "anonymous"), a2.alt = e2.name, a2.onerror = function() {
f(n2) && n2("\u56FE\u7247\u52A0\u8F7D\u9519\u8BEF\uFF01");
}, i2) {
var o2 = i2.createObjectURL(e2);
a2.onload = function() {
t2(a2), i2.revokeObjectURL(o2);
}, a2.src = o2;
} else
this.file2DataUrl(e2, function(e3) {
a2.onload = function() {
t2(a2);
}, a2.src = e3;
}, n2);
}, o.url2Image = function(e2, t2, n2) {
var r2 = new Image();
r2.src = e2, r2.onload = function() {
t2(r2);
}, r2.onerror = function() {
f(n2) && n2("\u56FE\u7247\u52A0\u8F7D\u9519\u8BEF\uFF01");
};
}, o.image2Canvas = function(e2, t2, n2, r2, a2, i2, o2) {
var s2 = document.createElement("canvas"), c2 = s2.getContext("2d");
return s2.width = i2 || e2.naturalWidth, s2.height = o2 || e2.naturalHeight, f(r2) && r2(c2, s2), c2.save(), c2.drawImage(e2, 0, 0, t2, n2), c2.restore(), f(a2) && a2(c2, s2), s2;
}, o.canvas2DataUrl = function(e2, t2, n2) {
return e2.toDataURL(n2 || "image/jpeg", t2);
}, o.dataUrl2Image = function(e2, t2, n2) {
var r2 = new Image();
r2.onload = function() {
t2(r2);
}, r2.error = function() {
f(n2) && n2("\u56FE\u7247\u52A0\u8F7D\u9519\u8BEF\uFF01");
}, r2.src = e2;
}, o.dataUrl2Blob = function(e2, t2) {
for (var n2 = e2.split(",")[1], r2 = e2.match(/^data:(.*?)(;base64)?,/)[1], a2 = atob(n2), i2 = n2.length, o2 = new Uint8Array(i2), s2 = 0; s2 < i2; s2++)
o2[s2] = a2.charCodeAt(s2);
return new Blob([o2], { type: t2 || r2 });
}, o.blob2DataUrl = function(e2, t2, n2) {
this.file2DataUrl(e2, t2, n2);
}, o.blob2Image = function(e2, t2, n2) {
this.file2Image(e2, t2, n2);
}, o.canvas2Blob = function(e2, t2, n2, r2) {
var a2 = this;
HTMLCanvasElement.prototype.toBlob || Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", { value: function(e3, t3, n3) {
var r3 = this.toDataURL(t3, n3);
e3(a2.dataUrl2Blob(r3));
} }), e2.toBlob(function(e3) {
t2(e3);
}, r2 || "image/jpeg", n2 || 0.8);
}, o.upload = function(e2, t2, n2) {
var r2 = new XMLHttpRequest(), a2 = new FormData();
a2.append("file", t2), r2.onreadystatechange = function() {
if (4 !== r2.readyState || 200 !== r2.status)
throw new Error(r2);
n2 && n2(r2.responseText);
}, r2.open("POST", e2, true), r2.send(a2);
}, o)
o.hasOwnProperty(h) && (l[h] = o[h]);
}]).default;
});
})(imageCompressor_min);
{
jsImageCompressor.exports = imageCompressor_min.exports;
}
var ImageCompressor = jsImageCompressor.exports;
export { ImageCompressor as I };