小程序云开发,图片处理
发布于 3 年前 作者 nkang 1554 次浏览 来自 分享

一、应用小程序云函数,进行图片检测处理

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html#method-cloud

1)问题:图片大小限制1M ,图片尺寸不超过 750px x 1334px ;

2)解决:上传云存储,然后使用云函数进行检测

二、相册或是拍照后的图片,1)上传到云存储后 2)cloud替换成https开头的url 3) 云开发CloudBase图像处理扩展

https://cloud.tencent.com/document/product/876/42103 4) 再进行图片安全检测

  wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const tempFilePaths = res.tempFilePaths[0]
        const cloudPath = `act/original-${Math.floor(Math.random() * 1000000000)}.jpg`
        wx.cloud.uploadFile({
          cloudPath,
          filePath: tempFilePaths
        }).then(res => {
          console.log('原临时路径换原图云存储路径', res)
          const pictureOrignial = res.fileID
          wx.cloud.getTempFileURL({ // cloud替换成https开头的url
            fileList: [pictureOrignial],
            success: res => {
              console.log(res)
              const url = res.fileList[0].tempFileURL
              this.setData({
                src: `${url}?imageView2/2/w/258/h/258`
              }, () => {
                wx.getImageInfo({
                  src: this.data.src,
                  success: res => {
                    console.log('检测的图片信息', res, res.width, res.height)
                    wx.getFileInfo({
                      filePath: res.path,
                      success: res1 => {
                        console.log('检测的图片的大小', res1.size)
                        handleImg.checkImg(res.path).then(res => { // 图片检测封装成工具函数,多处需要调用
                          console.log(res)
                          // if (res === 'ok') {
                          //   wx.hideLoading()
                          //   getApp().globalData.imgSrc = tempFilePaths
                          // } else {
                          //   wx.hideLoading()
                          //   utils.showTip()
                          //   utils.backNoReloadPage()
                          // }
                        })
                      }
                    })
                  }
                })
              })
            }
          })
        }).catch(err => {
          console.log(err)
          wx.hideLoading()
        })
      }
    })
回到顶部