UNPKG

@kedao/finder

Version:

Media manager for Kedao Editor.

44 lines (37 loc) 1.12 kB
export const compressImage = async (url, width = 1280, height = 800) => { return await new Promise((resolve, reject) => { const image = new Image() image.src = url image.onerror = function (error) { reject(error) } image.onload = function (this: HTMLImageElement) { try { const compressCanvas = document.createElement('canvas') const scale = this.width > width || this.height > height ? this.width > this.height ? width / this.width : height / this.height : 1 compressCanvas.width = this.width * scale compressCanvas.height = this.height * scale const canvasContext = compressCanvas.getContext('2d') canvasContext.drawImage( this, 0, 0, compressCanvas.width, compressCanvas.height ) resolve({ url: compressCanvas.toDataURL('image/png', 1), width: compressCanvas.width, height: compressCanvas.height }) } catch (error) { reject(error) } } }) }