web前端压缩图片并上传

开发项目的时候碰到一个需求,由于用户的上传图片过大,需在上传前先将图片压缩到指定尺寸大小,再进行上传

实现步骤关键代码:

export const compressImg = (file: File): Promise<File> => {
  return new Promise((resolve) => {
    if (file.size > 3 * 1024 * 1024) {
      const reader = new FileReader(),
        name = file.name
      // 生成canvas画布
      const canvas = document.createElement('canvas')
      const context = canvas.getContext('2d')
      reader.readAsDataURL(file) //转base64
      reader.onload = function(e) {
        const image = new Image()
        image.src = e.target && e.target.result ? e.target.result.toString() : ''
        image.onload = function() {
          // 图片原始尺寸
          const originWidth = image.width
          const originHeight = image.height
          // 设置最大尺寸限制
          const maxWidth = 3500
          const maxHeight = 3500
          // 设置目标尺寸
          let targetWidth = originWidth
          let targetHeight = originHeight
          // 图片尺寸超过1000x1000限制
          if (originWidth > maxWidth || originHeight > maxHeight) {
            if (originWidth / originHeight > maxWidth / maxHeight) {
              // 更宽 按照宽度限定尺寸
              targetWidth = maxWidth
              targetHeight = Math.round(maxWidth * (originHeight / originWidth))
            } else {
              // 更高 按照高度限定尺寸
              targetHeight = maxHeight
              targetWidth = Math.round(maxHeight * (originWidth / originHeight))
            }
          }
          // canvas对图片进行缩放
          canvas.width = targetWidth
          canvas.height = targetHeight
          // 清除画布
          if (context) context.clearRect(0, 0, targetWidth, targetHeight)
          // 将图片划到canvas上
          if (context) context.drawImage(image, 0, 0, targetWidth, targetHeight)
          // data = canvas.toDataURL('image/jpeg', 0.86) // 压缩比例0.86,结果接近减小了一半
          const data = canvas.toDataURL('image/jpeg', 0.6) // 压缩比例0.6
          let arr = data.split(','),
            mime = (arr as any)[0].match(/:(.*?);/)[1], // 转成blob
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n)
          while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
          }
          const files = new File([u8arr], name, { type: mime }) // 转成file
          resolve(files)
        }
      }
    } else {
      resolve(file)
    }
  })
}

这里将压缩方法进行了封装。
原理是将图片转为canvas,然后进行压缩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值