Browse Source

新增图片剪裁

Limengbo 6 years ago
parent
commit
b0e44c76c5

+ 1 - 0
app.json

@@ -5,6 +5,7 @@
     "pages/setName/setName",
     "pages/album/album",
     "pages/clip/clip",
+    "pages/clipPhoto/clipPhoto",
     "pages/childMatch/childMatch",
     "pages/access/access",
     "pages/Instructions/Instructions"

+ 1 - 0
pages/Instructions/Instructions.wxss

@@ -33,4 +33,5 @@
 
 .Explain image {
    width: 100%;
+   height:460rpx;
 }

+ 4 - 2
pages/access/access.js

@@ -7,7 +7,8 @@ Page({
    */
   data: {
     PhotoBoxInfo: {},
-    likeStatus: false
+    likeStatus: false,
+    avatar: ""
   },
 
   /**
@@ -21,7 +22,8 @@ Page({
   getPhoneBoxInfo(uid){
     httpRequestApi.getPhotoBoxInfoById({ uid }).success((res) => {
       this.setData({
-        PhotoBoxInfo: res.data.data
+        PhotoBoxInfo: res.data.data,
+        avatar: res.data.data.user.avatar
       })
     })
   },

+ 1 - 1
pages/access/access.wxml

@@ -1,7 +1,7 @@
 <!--pages/access/access.wxml-->
 <view class="access">
     <view class="information-item">
-        <image class="head" src="{{myData.avatar ? myData.avatar : '../../static/image/default.png'}}"></image>
+        <image class="head" src="{{avatar ? avatar : '../../static/image/default.png'}}"></image>
         <view class="access-name">
             <text class="name">{{PhotoBoxInfo.user.nickName}}</text>
             <text>{{PhotoBoxInfo.user.likeCount}}赞</text>

+ 8 - 8
pages/album/album.js

@@ -42,7 +42,7 @@ Page({
       return false;
     };
     wx.chooseImage({
-      count: 9, // 默认9
+      count: 1, // 默认9
       sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
       sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
       success:  (res) => {
@@ -55,8 +55,11 @@ Page({
           mask: true,  
           duration: 1000 
         }) 
-        const that = this;
-        let uploadImgCount = 0;  
+        wx.navigateTo({
+          url: '/pages/clipPhoto/clipPhoto?imageUrl=' + tempFilePaths[0] 
+        })
+        /*
+        const that = this; 
         tempFilePaths.forEach(item => {
           //上传文件
           wx.uploadFile({  
@@ -84,11 +87,7 @@ Page({
                     content: '图片上传到相册失败'
                   }) 
                 });
-              }
-              //如果是最后一张,则隐藏等待中  
-              if (uploadImgCount == tempFilePaths.length) {  
-                wx.hideToast();  
-              }  
+              } 
             },  
             fail: function (res) {  
               wx.hideToast();  
@@ -99,6 +98,7 @@ Page({
             }  
           });  
         });
+        */
       }
     })
   },

+ 212 - 0
pages/clipPhoto/clipPhoto.js

@@ -0,0 +1,212 @@
+// pages/clip/clip.js
+const HOST = require('../../utils/const.js').apiUrl;
+import httpRequestApi from '../../utils/APIRequest';
+Page({
+
+  /**
+   * 页面的初始数据
+   */
+  data: {
+    imageUrl: '',
+    cropperW: '',
+    cropperH: '',
+    img_ratio: '',
+    IMG_W: '',
+    IMG_H: '',
+    clipImage: '',
+    left: '',
+    top: '',
+    screenW: '',
+    screenH: ''
+  },
+  //拖拽事件
+  move: function ({ detail }) {
+    this.setData({
+      left: detail.x * 2,
+      top: detail.y * 2
+    })
+  },
+  //生成图片
+  getImageInfo: function () {
+    wx.showLoading({
+      title: '图片生成中...',
+    })
+    const img_ratio = this.data.img_ratio;
+    const canvasW = (this.data.screenW / this.data.cropperW) * this.data.IMG_W
+    const canvasH = (this.data.screenH / this.data.cropperH) * this.data.IMG_H
+    const canvasL = 0;
+    const canvasT = (this.data.top / this.data.cropperH) * this.data.IMG_H
+    // 将图片写入画布
+    const ctx = wx.createCanvasContext('myCanvas');
+    //绘制图像到画布       
+    ctx.beginPath(); //开始绘制 
+    //判断是竖着显示还是横着显示
+    if(this.data.img_ratio >= 1) {
+      //保存图片分别是图片地址,canvas绘制距离左边距离,距离上边距离,截取原图片宽度,原图片高度,图片在canvas上的左位置,上位置,生成图片的宽,生成图片的高;
+      ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, 0, 0, this.data.screenW / 2, this.data.screenH / 2);
+
+    } else {
+      //如果图片比较窄两边留白,留白距离
+      const liubaiLeft = (this.data.screenW / 2 - this.data.cropperW / 2) /2;
+      //保存图片分别是图片地址,canvas绘制距离左边距离,距离上边距离,截取原图片宽度,原图片高度,图片在canvas上的左位置,上位置,生成图片的宽,生成图片的高;
+      ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, liubaiLeft, 0, this.data.cropperW, this.data.screenH * img_ratio);
+    } 
+    ctx.draw(true, () => {
+      // 获取画布要裁剪的位置和宽度   
+      wx.canvasToTempFilePath({
+        x: 0,
+        y: 0,
+        width: this.data.screenW / 2,
+        height: this.data.screenH / 2,
+        destWidth: this.data.screenW / 2,
+        destHeight: this.data.screenH / 2,
+        quality: 0.5,
+        canvasId: 'myCanvas',
+        success:  (res) => {
+          wx.hideLoading()
+          console.log(res);
+          this.setData({
+            clipImage: res.tempFilePath
+          })
+
+          //上传文件
+          wx.uploadFile({  
+            url: HOST + 'wx/file/upload',  
+            filePath: res.tempFilePath,  
+            name: 'uploadfile_ant',  
+            header: {  
+              "Content-Type": "multipart/form-data"  
+            },  
+            success: (res) => {  
+              const data = JSON.parse(res.data);
+              if(data.success) {
+                //上传文件成功后放到相册里
+                httpRequestApi.addPhotoList({
+                  path: data.data
+                }).success((res) => {
+                  wx.navigateTo({
+                    url: '/pages/album/album' 
+                  })
+                }).fail(() => {
+                  wx.showModal({  
+                    title: '错误提示',  
+                    content: '图片上传到相册失败'
+                  }) 
+                });
+              } 
+            },  
+            fail: function (res) {  
+              wx.hideToast();  
+              wx.showModal({  
+                title: '错误提示',  
+                content: '上传图片失败'
+              })  
+            }  
+          }); 
+          //成功获得地址的地方
+          // wx.previewImage({
+          //   current: '', // 当前显示图片的http链接
+          //   urls: [res.tempFilePath] // 需要预览的图片http链接列表
+          // })
+        }
+      })
+    })
+  },
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad: function (options) {
+    console.log(options.imageUrl);
+    //获取初始屏幕宽度
+    const screenW = wx.getSystemInfoSync().windowWidth * 2;
+    //h获取剪裁框的高度,按照16:9来计算高度
+    const screenH = screenW / 16 * 9; 
+    console.log('剪裁框宽高',screenW,screenH);
+    this.setData({
+      screenW,
+      screenH
+    })
+    const imageUrl = options.imageUrl;
+    this.setData({
+      imageUrl
+    })
+    wx.getImageInfo({
+      src: imageUrl,
+      success: (res) => {
+        console.log(res);
+        //图片实际款高
+        const width = res.width;
+        const height = res.height;
+        //图片宽高比例
+        const img_ratio = width / height
+        this.setData({
+          img_ratio,
+          IMG_W: width,
+          IMG_H: height,
+        })
+        if (img_ratio >= 1) {
+          //宽比较大,横着显示
+          this.setData({
+            cropperW: 750,
+            cropperH: 750 / img_ratio,
+          })
+        } else {
+          //竖着显示
+          this.setData({
+            cropperW: 750 * img_ratio,
+            cropperH: 750
+          })
+        }
+      } 
+    })
+  },
+
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady: function () {
+
+  },
+
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow: function () {
+
+  },
+
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide: function () {
+
+  },
+
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload: function () {
+
+  },
+
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh: function () {
+
+  },
+
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom: function () {
+
+  },
+
+  /**
+   * 用户点击右上角分享
+   */
+  onShareAppMessage: function () {
+
+  }
+})

+ 1 - 0
pages/clipPhoto/clipPhoto.json

@@ -0,0 +1 @@
+{}

+ 12 - 0
pages/clipPhoto/clipPhoto.wxml

@@ -0,0 +1,12 @@
+<!--pages/clip/clip.wxml-->
+<view class="clip">
+    <image class="head-img" style="width:{{cropperW}}rpx;height:{{cropperH}}rpx" src="{{imageUrl}}"></image>
+    <movable-area scale-area style="width:100%;height:{{cropperH}}rpx">
+        <movable-view style="width: 100%; height: {{screenH}}rpx;" bindchange="move" direction="all" scale scale-min="0.5" scale-max="1.8">
+        </movable-view>
+    </movable-area>
+    <view class="btn">
+        <text bindtap="getImageInfo">保存</text>
+    </view>
+</view>
+<canvas canvas-id="myCanvas" style="position:absolute; width:100%;height:100%;border: 1px solid red;left: -9999px; top: -9999px;"></canvas>

+ 42 - 0
pages/clipPhoto/clipPhoto.wxss

@@ -0,0 +1,42 @@
+/* pages/clip/clip.wxss */
+.clip {
+    position: relative;
+    width: 100%;
+    height: 100%;
+}
+
+.head-img,
+movable-area {
+    position: absolute;
+    left: 50%;
+    top: 20%;
+    transform: translateX(-50%);  
+}
+
+movable-view {
+    border: 4rpx solid #fe9d00;
+    box-sizing: border-box;
+}
+
+
+.btn {
+    position: absolute;
+    left: 0;
+    bottom: 10rpx;
+    display: flex;
+    justify-content: space-around;
+    align-items: center;
+    height: 88rpx;
+    width: 100%;
+}
+
+.btn text {
+   width: 40%;
+   height: 100%;
+   border-radius: 10rpx;
+   display: flex;
+   align-items: center;
+   justify-content: center;
+   background: skyblue;
+   color: #fff; 
+}