码迷,mamicode.com
首页 > 其他好文 > 详细

canvas压缩图片

时间:2019-02-24 18:47:04      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:ssim   图片压缩   com   javascrip   call   draw   image   ali   比例   

    // 图片压缩,默认同比例压缩
    compressImg (URL, callback) {
      // 生成canvas
      let canvas = document.createElement(‘canvas‘)
      let ctx = canvas.getContext(‘2d‘)
      let image = new Image()
      image.setAttribute(‘crossOrigin‘, ‘anonymous‘)
      image.src = URL
      let quality = 0.3
      image.onload = () => {
        canvas.width = image.width
        canvas.height = image.height
        ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
        let newImageData = canvas.toDataURL(‘image/jpeg‘, quality)
        callback(newImageData)
      }
    }

  

canvas压缩图片

标签:ssim   图片压缩   com   javascrip   call   draw   image   ali   比例   

原文地址:https://www.cnblogs.com/liumengdie/p/10427129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!