ant-design-x-vue
Version:
Craft AI-driven interfaces effortlessly
32 lines (31 loc) • 1.22 kB
JavaScript
const E = (e) => e.indexOf("image/") === 0;
function g(e) {
return new Promise((a) => {
if (!e || !e.type || !E(e.type)) {
a("");
return;
}
const n = new Image();
if (n.onload = () => {
const { width: t, height: c } = n, o = t / c, i = o > 1 ? 200 : 200 * o, r = o > 1 ? 200 / o : 200, s = document.createElement("canvas");
s.width = i, s.height = r, s.style.cssText = `position: fixed; left: 0; top: 0; width: ${i}px; height: ${r}px; z-index: 9999; display: none;`, document.body.appendChild(s), s.getContext("2d").drawImage(n, 0, 0, i, r);
const d = s.toDataURL();
document.body.removeChild(s), window.URL.revokeObjectURL(n.src), a(d);
}, n.crossOrigin = "anonymous", e.type.startsWith("image/svg+xml")) {
const t = new FileReader();
t.onload = () => {
t.result && typeof t.result == "string" && (n.src = t.result);
}, t.readAsDataURL(e);
} else if (e.type.startsWith("image/gif")) {
const t = new FileReader();
t.onload = () => {
t.result && a(t.result);
}, t.readAsDataURL(e);
} else
n.src = window.URL.createObjectURL(e);
});
}
export {
E as isImageFileType,
g as previewImage
};