javascript图片压缩

144 阅读1分钟
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)
        }
    })
}