图片地址
可选配置
图片压缩选项
Optionalcompressor?: (自定义压缩函数,用于覆盖默认压缩行为
Optionalfetcher?: (url: string) => Promise<Response>自定义 fetch 函数,用于使用自己封装的请求库读取图片 必须返回符合 Web 标准的 Response 对象
Optionalquality?: number压缩比率,默认 0.92
// 基本用法(浏览器自动使用 Canvas 压缩,Node.js 自动检测并使用 sharp)
imageUrlToBase64("https://example.com/image.jpg");
// 使用自定义 fetch 函数(如 axios 封装)
imageUrlToBase64("https://example.com/image.jpg", {
fetcher: async (url) => {
// 使用 axios 或其他请求库,但必须返回 Response 对象
const response = await axios.get(url, { responseType: 'arraybuffer' });
return new Response(response.data, {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
}
});
图片地址转 base64 数据