校验图片尺寸

 

const validImageSize = (ext, rawFile) => {
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = URL.createObjectURL(rawFile)
        let validRes = false
        try {
          img.onload = () => {
            if (img.width !== IMG_SIZE_WIDTH || img.height !== IMG_SIZE_HEIGHT) {
              ElMessage.error('上传的图片尺寸不正确')
              validRes = false
            } else if (!IconImageType.includes(ext)) {
              ElMessage.error('不支持的图片格式!')
              validRes = false
            } else {
              validRes = true
            }
            resolve(validRes)
          }
        } catch (err) {
          reject(err)
        }
      })
    }

你可能感兴趣的:(前端,javascript,算法)