使用Canvas API实现前端图片压缩记录

前言

最近我在工作上遇到用户上传大尺寸图片的需求,发现直接上传原始图片会消耗过多带宽和存储空间。于是在网上找了一些像 图片压缩网站 这样的工具能在浏览器里直接完成压缩,无需上传到服务器,这让我很受启发。经过研究,我找到了使用Canvas API的简单实现方案,下面是自己实现的代码和想法。

核心实现原理

Canvas图片压缩主要利用以下技术流程:

  1. 将原始图片绘制到Canvas画布上
  2. 调整Canvas尺寸以实现分辨率压缩
  3. 使用有损编码(如JPEG)和可调质量参数输出图片
  4. 将结果转换为Blob或Base64格式

关键代码实现

1. 图片加载与绘制

javascript 复制代码
function loadImage(file) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    const url = URL.createObjectURL(file);
    
    img.onload = () => {
      URL.revokeObjectURL(url);
      resolve(img);
    };
    
    img.onerror = (e) => {
      reject(e);
    };
    
    img.src = url;
  });
}

2. 核心压缩函数

javascript 复制代码
async function compressImage(file, options = {}) {
  const { 
    quality = 0.8, 
    maxWidth = 800, 
    maxHeight = 800,
    mimeType = 'image/jpeg'
  } = options;
  
  // 加载原始图片
  const img = await loadImage(file);
  
  // 计算缩放比例
  const [newWidth, newHeight] = calculateSize(img, maxWidth, maxHeight);
  
  // 创建canvas并绘制
  const canvas = document.createElement('canvas');
  canvas.width = newWidth;
  canvas.height = newHeight;
  
  const ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0, newWidth, newHeight);
  
  // 转换为Blob对象
  return new Promise((resolve) => {
    canvas.toBlob(
      (blob) => resolve(blob),
      mimeType,
      quality
    );
  });
}

// 辅助函数:计算新尺寸
function calculateSize(img, maxWidth, maxHeight) {
  let width = img.width;
  let height = img.height;
  
  // 计算缩放比例
  if (width > height) {
    if (width > maxWidth) {
      height = Math.round((height * maxWidth) / width);
      width = maxWidth;
    }
  } else {
    if (height > maxHeight) {
      width = Math.round((width * maxHeight) / height);
      height = maxHeight;
    }
  }
  
  return [width, height];
}

3. 使用示例

javascript 复制代码
const fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', async (e) => {
  const file = e.target.files[0];
  if (!file) return;
  
  try {
    const compressedBlob = await compressImage(file, {
      quality: 0.7,
      maxWidth: 1024,
      maxHeight: 1024
    });
    
    // 使用压缩后的Blob
    console.log(`原始大小: ${file.size} bytes`);
    console.log(`压缩后: ${compressedBlob.size} bytes`);
    
    // 可以上传到服务器或提供给用户下载
  } catch (error) {
    console.error('压缩失败:', error);
  }
});

关键参数说明

  1. quality (0-1) : 控制JPEG压缩质量,数值越小压缩率越高
  2. maxWidth/maxHeight: 限制图片最大尺寸,实现分辨率压缩
  3. mimeType: 可指定为'image/jpeg'(有损)、'image/png'(无损)等

浏览器兼容性

Canvas压缩功能在现代浏览器中支持良好,包括:

  • Chrome 50+
  • Firefox 50+
  • Safari 10+
  • Edge 15+
相关推荐
其实防守也摸鱼9 小时前
网安自测题:掌握核心知识点的实用练习
linux·运维·服务器·前端·数据库·sql·xss
geovindu12 小时前
css: Timeline scribble
前端·css·iphone
沙漠之主20 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
hasty20 小时前
不上传新包,也能改变用户拿到的版本:npm dist-tag 的 OIDC 权限治理
前端·npm·node.js
Csvn20 小时前
diff 算法(虚拟 DOM Reconciliation)
前端
前端snow1 天前
ai agent --- 文件存储
前端
Frag0ut1 天前
Chrome四大版本获取及共存指南:Stable/Beta/Dev/Canary
前端·chrome·浏览器·dev·beta·canary·共存版
IT_陈寒1 天前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
广州华水科技1 天前
单北斗GNSS变形监测在大坝安全监测中的应用与优势
前端
zeng不错1 天前
B站 173 个投稿活动看到眼瞎?我写了个扩展,主打一个精准打击
前端