封装图片压缩

1.可以设置压缩后的图片最大宽度/高度。 限制输出最大宽/高值 (如果不设置,按照原图尺寸大小)
2.要求输出格式支持blob 和 File 文件。
3.可以设置输出图片的质量级别(假设1-10级别。级别越大,质量越高,占用存储越大,越清晰)

  • 首先判断 maxWidth 和 maxHeight 是否都有定义。
  • 如果图片的宽度 (width) 或者高度 (height) 超过了指定的最大值,则需要进行缩放。
  • 计算缩放比例 (ratio),使用 Math.min 函数来确定应用于宽度和高度的最小比例,以确保图片在两个维度上都适应指定的最大限制。
  • 将原始的宽度和高度乘以这个比例,从而使图片缩放到适当的大小。
  • 如果只有 maxWidth 有定义,并且图片宽度 (width) 超过了 maxWidth,则需要根据比例调整高度 (height),以确保图片不会超过指定的最大宽度。
  • 如果只有 maxHeight 有定义,并且图片高度 (height) 超过了 maxHeight,则需要根据比例调整宽度 (width),以确保图片不会超过指定的最大高度。

 代码如下:

export function compressImage(options: any) {
  const { file, photoType, quality = 10, maxWidth, maxHeight } = options;
  return new Promise((resolve, reject) => {
    const image = new Image();
    image.onload = () => {
      const canvas = document.createElement('canvas');
      let width = image.width;
      let height = image.height;
      if (maxWidth && maxHeight) {
        if (width > maxWidth || height > maxHeight) {
          const ratio = Math.min(maxWidth / width, maxHeight / height);
          console.log(ratio, 'ratio');
          width *= ratio;
          height *= ratio;
        }
      } else if (maxWidth && width > maxWidth) {
        height *= maxWidth / width;
        width = maxWidth;
      } else if (maxHeight && height > maxHeight) {
        width *= maxHeight / height;
        height = maxHeight;
      }
      canvas.width = width;
      canvas.height = height;
      const ctx = canvas.getContext('2d');
      if (!ctx) {
        reject(new Error('NOT 2D canvas'));
        return;
      }
      ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
      canvas.toBlob(
        (blob) => {
          if (!blob) {
            reject(new Error('err blob'));
            return;
          }
          if (photoType === 'blob') {
            resolve(blob);
          } else if (photoType === 'file') {
            const compressedFile = new File([blob], file.name, { type: blob.type });
            resolve(compressedFile);
          } else {
            reject(new Error('err type'));
          }
        },
        file.type,
        quality / 10
      );
    };

    image.onerror = () => {
      reject(new Error('onerrr'));
    };
    image.src = URL.createObjectURL(file);
  });
}

 

相关推荐

  1. 封装图片压缩

    2024-07-13 09:26:04       22 阅读
  2. vue图片压缩

    2024-07-13 09:26:04       37 阅读
  3. python 读图片封装

    2024-07-13 09:26:04       40 阅读
  4. kotlin图片合成和压缩

    2024-07-13 09:26:04       42 阅读
  5. uniapp-image-compress 图片压缩

    2024-07-13 09:26:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 09:26:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 09:26:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 09:26:04       57 阅读
  4. Python语言-面向对象

    2024-07-13 09:26:04       68 阅读

热门阅读

  1. 为什么文件需要校验MD5?

    2024-07-13 09:26:04       23 阅读
  2. STL内建仿函数

    2024-07-13 09:26:04       23 阅读
  3. 开源 Wiki 系统 InfoSphere 2024.01.1 发布

    2024-07-13 09:26:04       29 阅读
  4. macOS 的电源适配器设置

    2024-07-13 09:26:04       25 阅读
  5. PTA 7-14 畅通工程之局部最小花费问题

    2024-07-13 09:26:04       27 阅读
  6. Vue单路由的独享守卫怎么设置

    2024-07-13 09:26:04       26 阅读
  7. 代码随想录算法训练营第33天

    2024-07-13 09:26:04       25 阅读