UNPKG

@mt-kit/utils

Version:
27 lines 1.13 kB
/** * 将图像 URL 转换为 base64 编码的字符串 * @param url 是要转换的图像的 URL * @param mineType 是可选参数,用于指定生成的 base64 字符串的 MIME 类型,默认为 image/png * @returns 返回一个 Promise 对象,resolve 后的值是生成的 base64 编码的字符串 */ export default function imageUrlToBase64(url, mineType) { return new Promise((resolve, reject) => { let canvas = document.createElement("CANVAS"); const ctx = canvas === null || canvas === void 0 ? void 0 : canvas.getContext("2d"); const img = new Image(); img.crossOrigin = ""; img.addEventListener("load", () => { if (!canvas || !ctx) { return reject(new Error("Canvas or context is not available")); } canvas.height = img.height; canvas.width = img.width; ctx.drawImage(img, 0, 0); const dataUrl = canvas.toDataURL(mineType || "image/png"); canvas = null; resolve(dataUrl); }); img.src = url; }); } //# sourceMappingURL=index.js.map