js计算图片大小(promise)

const calculateSize = (imageUrl) => {
  return new Promise((resolve, reject) => {
    const img = new Image()
    img.onload = () =>
      resolve({
        width: img.width,
        height: img.height,
      })
    img.onerror = reject
    img.src = imageUrl
  })
}

 

// 使用
const getSize = await calculateSize(imageUrl)
console.log(getSize); // {width: 200, height: 200}

 

posted @ 2020-12-23 10:26  doublealoe  阅读(645)  评论(0编辑  收藏  举报