ant-design-x-vue-next
Version:
Ant Design X for Vue — community continuation aligned with @ant-design/x
36 lines (35 loc) • 1.3 kB
JavaScript
const E = (t) => t.indexOf("image/") === 0;
function m(t) {
return new Promise((o) => {
if (!t || !t.type || !E(t.type)) {
o("");
return;
}
const n = new Image();
if (n.onload = () => {
const { width: e, height: c } = n, a = e / c, r = a > 1 ? 200 : 200 * a, i = a > 1 ? 200 / a : 200, s = document.createElement("canvas");
s.width = r, s.height = i, s.style.cssText = `position: fixed; left: 0; top: 0; width: ${r}px; height: ${i}px; z-index: 9999; display: none;`, document.body.appendChild(s), s.getContext("2d").drawImage(n, 0, 0, r, i);
const d = s.toDataURL();
document.body.removeChild(s), window.URL.revokeObjectURL(n.src), o(d);
}, n.crossOrigin = "anonymous", t.type.startsWith("image/svg+xml")) {
const e = new FileReader();
e.onload = () => {
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.onload = () => {
e.result && o(e.result);
}, e.readAsDataURL(t);
} else
n.src = window.URL.createObjectURL(t);
});
}
function R(t, o) {
return o.has(t.substring(1).toLowerCase());
}
export {
E as isImageFileType,
R as matchExt,
m as previewImage
};