@nickyzj2023/utils
    Preparing search index...

    Function imageUrlToBase64

    • 图片地址转 base64 数据

      Parameters

      • imageUrl: string

        图片地址

      • options: ImageCompressionOptions = {}

        可选配置

        图片压缩选项

        • Optionalcompressor?: (
              arrayBuffer: ArrayBuffer,
              mime: string,
              quality: number,
          ) => Promise<string> | string

          自定义压缩函数,用于覆盖默认压缩行为

        • Optionalfetcher?: (url: string) => Promise<Response>

          自定义 fetch 函数,用于使用自己封装的请求库读取图片 必须返回符合 Web 标准的 Response 对象

        • Optionalquality?: number

          压缩比率,默认 0.92

      Returns Promise<string>

      // 基本用法(浏览器自动使用 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
      });
      }
      });
      // 使用自定义压缩函数覆盖默认行为
      imageUrlToBase64("https://example.com/image.jpg", {
      quality: 0.8,
      compressor: async (buffer, mime, quality) => {
      // 自定义压缩逻辑
      return `data:${mime};base64,...`;
      }
      });