javascript图片压缩

const compressImg = function (blobUrl, options) {
    return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = blobUrl
        img.onload = function () {
            const canvas = document.createElement('canvas')
            const ctx = canvas.getContext('2d')
            canvas.setAttribute('width', img.width)
            canvas.setAttribute('height', img.height)
            ctx.drawImage(img, 0, 0, img.width, img.height)
            canvas.toBlob(function (blob) {
                resolve(URL.createObjectURL(blob))
            }, options.type, options.scale)
        }
    })
}

  

posted @ 2019-09-06 11:19  朱现国  阅读(255)  评论(0编辑  收藏  举报