@artmate/chat
Version:
**开箱即用的AI组件库(基于 Vue3 + ElementPlus)**
37 lines (36 loc) • 1.3 kB
JavaScript
function p(t) {
return t.indexOf("image/") === 0;
}
const r = 200;
function E(t) {
return new Promise((i) => {
if (!t || !t.type || !p(t.type)) {
i("");
return;
}
const n = new Image();
if (n.addEventListener("load", () => {
const { width: e, height: g } = n, a = e / g, o = a > 1 ? r : r * a, d = a > 1 ? r / a : r, s = document.createElement("canvas");
s.width = o, s.height = d, s.style.cssText = `position: fixed; left: 0; top: 0; width: ${o}px; height: ${d}px; z-index: 9999; display: none;`, document.body.append(s);
const c = s.getContext("2d");
c == null || c.drawImage(n, 0, 0, o, d);
const m = s.toDataURL();
s.remove(), window.URL.revokeObjectURL(n.src), i(m);
}), n.crossOrigin = "anonymous", t.type.startsWith("image/svg+xml")) {
const e = new FileReader();
e.addEventListener("load", () => {
e.result && typeof e.result == "string" && (n.src = e.result);
}), e.readAsDataURL(t);
} else if (t.type.startsWith("image/gif")) {
const e = new FileReader();
e.addEventListener("load", () => {
e.result && i(e.result);
}), e.readAsDataURL(t);
} else
n.src = window.URL.createObjectURL(t);
});
}
export {
p as isImageFileType,
E as previewImage
};