vue中发布文章对首篇文章封面和次篇文章封面图片尺寸校验

vue中发布文章对首篇文章封面和次篇文章封面图片尺寸校验

根据后端返的url判断图片尺寸

  // 获取图片尺寸
    getImgSize(url) {
      return new Promise((resolve, reject) => {
        const imgObj = new Image()
        imgObj.src = url
        imgObj.onload = () => {
          resolve({
            width: imgObj.width,
            height: imgObj.height
          })
        }
      })
    },
  this.getImgSize(pic.url).then(res => {
        if (this.tuwenList.length === 1) {
          if (res.width / res.height === 2.35 / 1) {
            this.tuwenform.thumbMediaId = pic.mediaId
            this.tuwenform.thumbMediaUrl = pic.wxUrl
            this.$refs.addPic.dialogVisible = false
          } else {
            this.$message.error('首篇图文封面图片长宽比应为2.35:1')
          }
        } else {
          if (res.width / res.height === 1 / 1) {
            this.tuwenform.thumbMediaId = pic.mediaId
            this.tuwenform.thumbMediaUrl = pic.wxUrl
            this.$refs.addPic.dialogVisible = false
          } else {
            this.$message.error('次篇图文封面图片长宽比应为1:1')
          }
        }
      })

上传时的校验

 // 上传
    beforeupload(file) {
      const isJPG =
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isJPG) {
        this.$message.error('上传图片仅支持PNG\JPEG\JPG\GIF格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值