微信小程序 壓縮圖片並上傳


轉自https://segmentfault.com/q/1010000012507519

 

wxml寫入

<view bindtap='uploadImg'>上傳</view>
 <image bindtap='uploadImg' data-number="0" src="{{ uploadPic[0] }}" alt="" mode="widthFix"></image>
<canvas style="width: {{cw}}px; height: {{ch}}px;" canvas-id="firstCanvas"></canvas>

  

JS參考

 

 uploadImg(e) {
    let that = this;
    console.log(e);
    let index = e.currentTarget.dataset.number;
    let uploadFile = ''; //最后處理完,圖片上傳的圖片地址
    wx.chooseImage({
      success(res) {
        console.log(res)
        const tempFilePaths = res.tempFilePaths;

        //獲得原始圖片大小
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success(res) {
            // console.log('獲得原始圖片大小',res.width)
            //console.log(res.height)
            var originWidth, originHeight;
            originHeight = res.height;
            originWidth = res.width;
            console.log(originWidth);
            //壓縮比例
            // 最大尺寸限制
            var maxWidth = 1200,
              maxHeight = 600;
            // 目標尺寸
            var targetWidth = originWidth,
              targetHeight = originHeight;
            //等比例壓縮,如果寬度大於高度,則寬度優先,否則高度優先
            if (originWidth > maxWidth || originHeight > maxHeight) {
              if (originWidth / originHeight > maxWidth / maxHeight) {
                // 要求寬度*(原生圖片比例)=新圖片尺寸
                targetWidth = maxWidth;
                targetHeight = Math.round(maxWidth * (originHeight / originWidth));
              } else {
                targetHeight = maxHeight;
                targetWidth = Math.round(maxHeight * (originWidth / originHeight));
              }
            }
            //嘗試壓縮文件,創建 canvas
            var ctx = wx.createCanvasContext('firstCanvas');
            ctx.clearRect(0, 0, targetWidth, targetHeight);
            ctx.drawImage(tempFilePaths[0], 0, 0, targetWidth, targetHeight);
            ctx.draw();
            //更新canvas大小
            that.setData({
              cw: targetWidth,
              ch: targetHeight
            });
            //保存圖片
            setTimeout(function() {
              wx.canvasToTempFilePath({
                canvasId: 'firstCanvas',
                success: (res) => {
                  //寫入圖片數組
                  var uploadpic = "uploadPic[" + index + "]";
                  //
                  that.setData({
                    [uploadpic]: res.tempFilePath
                  });
                  uploadFile = res.tempFilePath;
                  //保存到相冊
                  // wx.saveImageToPhotosAlbum({
                  //   filePath: res.tempFilePath,
                  //   success: (res) => {
                  //     console.log(res)
                  //   },
                  //   fail: (err) => {
                  //     console.error(err)
                  //   }
                  // })
                  wx.uploadFile({
                    url: 'https://example.weixin.qq.com/upload', //僅為示例,非真實的接口地址
                    filePath: uploadFile,
                    name: 'file',
                    formData: {
                      'user': 'test'
                    },
                    success(res) {
                      const data = res.data
                      //do something
                    }
                  })
                },
                fail: (err) => {
                  console.error(err)
                }
              }, this)
            }, 500);




          }
        })




      }
    })
  }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM