小程序圖片裁剪

兩種方式:we-cropper 和 image-cropper
we-cropper在小程序android選擇圖片后跳轉(zhuǎn)到裁剪頁面圖片不能顯示函似,目前沒找到解決方法撇寞,項目中換了image-cropper做。
image-cropper如果需要動態(tài)改變裁剪框大小的時候蔑担,初始化比較慢。其他暫時沒發(fā)現(xiàn)問題啤握。
個人覺得image-cropper更好用

we-cropper

參考文檔https://we-plugin.github.io/we-cropper/#/
在templates添加we-cropper文件排抬。

image.png

在page里面新建文件夾,按照文檔給出的方式寫就好了

mport WeCropper from '../../templates/we-cropper/we-cropper.js'
const myApp = getApp()

const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

Page({
  data: {
    cropperOpt: {
      id: 'cropper', //用于收拾操作的canvas組件標識符
      targetId: 'targetCropper', // 用于用于生成截圖的canvas組件標識符
      pixelRatio: device.pixelRatio, // 傳入設備像素比
      width,
      height,
      scale: 2.5, // 最大縮放倍數(shù)
      zoom: 8, // 縮放系數(shù)
      cut: { // 裁剪框x軸起點   裁剪框y軸期起點
        x: 0,
        y: (height - 300) / 2,
        width: width, // 裁剪框?qū)挾?        height: 300 // 裁剪框高度
      },
      boundStyle: {
        color: 'rgba(0,0,0,0.8)',
        lineWidth: 1
      },
    }
  },
  onLoad(option) {
    console.log(option);
    const widthHeightScale = option.width / option.height;
    const {
      cropperOpt
    } = this.data;
    const filePath = decodeURIComponent(option.filePath); //圖片臨時地址


    cropperOpt.cut = {
      x: (width - width * option.width / 750) / 2,
      y: (height - width * option.height / 750) / 2,
      width: width * option.width / 750,
      height: width * option.height / 750,
    };
    this.setData({
      cropperOpt
    })
    this.cropper = new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        wx.showToast({
          title: '加載中',
          icon: 'loading',
          mask: true,
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        wx.hideToast()
      })
    //  獲取裁剪圖片資源后,給data添加src屬性及其值
    this.cropper.pushOrign(filePath);
  },
  touchStart(e) {
    this.cropper.touchStart(e)
  },
  touchMove(e) {
    this.cropper.touchMove(e)
  },
  touchEnd(e) {
    this.cropper.touchEnd(e)
  },
  getCropperImage() {
    let _this = this;
    this.cropper.getCropperImage()
      .then((src) => {
        console.log(src);
        // wx.previewImage({
        //   current: '', // 當前顯示圖片的http鏈接
        //   urls: [src] // 需要預覽的圖片http鏈接列表
        // });
        wx.showLoading({
          title: '上傳中...',
          mask: true,
        });

        wx.uploadFile({
          url: myApp.globalData.serverUrl + 'extEntryImageUpload.ext', //接口地址
          filePath: src,
          header: myApp.globalData.requestHeader,
          name: 'imageFile',
          success(res) {
     
          },
          fail(err) {
            //console.log(err);
          },
          complete: function () {
            wx.hideLoading();
          }
        })

      })
      .catch(() => {
        console.log('獲取圖片地址失敗缘薛,請稍后重試')
      })
  },

})
<import src="../../templates/we-cropper/we-cropper.wxml"/>

<view class="cropper-wrapper">
  <template is="we-cropper" data="{{...cropperOpt}}"/>
  <view class="cropper-buttons flex flex-align-center flex-pack-center">
    <view
      class="getCropperImage btn btn_bg"
      bindtap="getCropperImage">
      發(fā)布圖片
    </view>
  </view>
image-cropper

參考https://github.com/wx-plugin/image-cropper
git上的代碼基本上直接可以拿來用了
同樣在templates里面

image.png

page下新建文件夾
json

"usingComponents": {
    "image-cropper": "../../templates/image-cropper/imageCropper"
  },

wxml

<image-cropper id="image-cropper" limit_move="{{true}}" disable_rotate="{{true}}" width="{{width}}" height="{{height}}" imgSrc="{{src}}" limit_move="{{limit_move}}" disable_width="{{disable_width}}" disable_height="{{disable_height}}" bindload="cropperload" bindimageload="loadimage" bindtapcut="clickcut"></image-cropper>

<view class='btn flex flex-align-center flex-pack-justify'>
  <view class='subbtn left' catchtouchstart='rotate' catchtouchend='end' data-type="rotate">旋轉(zhuǎn)</view>
  <view class='subbtn' bindtap='submit'>上傳</view>
</view>

js

const app = getApp()
import {
  SERVERS_INFO, SERVERS_ALIAS, PATHS_ALIAS
} from '../../utils/api.js'
import utils from '../../utils/util.js';

const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    fileType: '',//學歷:E, 車:C, 房:H, 頭像:H, 用戶圖片:U,興趣愛好:I
    isupdate: '',//是否更新照片
    src: '',
    width: 380,//寬度
    height: 316,//高度
    limit_move: true,//是否禁用旋轉(zhuǎn)
    max_width: 250,
    max_height: 250,
    disable_width: true,
    disable_height: true
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function (option) {
    //獲取到image-cropper對象
    this.cropper = this.selectComponent("#image-cropper");
    const filePath = decodeURIComponent(option.filePath); //圖片臨時地址
    console.log(width * option.width / 750)
    console.log(width)
    //開始裁剪
    this.setData({
      src: filePath,
      width: width * option.width / 750,
      height: width * option.height / 750,
      max_width: width * option.width / 750,
      max_height: width * option.height / 750,
      fileType: option.fileType,
      isupdate: option.isupdate
    });
    wx.showLoading({
      title: '加載中'
    })
  },
  cropperload(e) {
    console.log("cropper初始化完成");
  },
  loadimage(e) {
    console.log("圖片加載完成", e.detail);
    wx.hideLoading();
    //重置圖片角度表锻、縮放、位置
    this.cropper.imgReset();
  },
  clickcut(e) {
    console.log(e.detail);
    //點擊裁剪框閱覽圖片
    wx.previewImage({
      current: e.detail.url, // 當前顯示圖片的http鏈接
      urls: [e.detail.url] // 需要預覽的圖片http鏈接列表
    })
  },
  submit: function () {
    this.cropper.getImg((obj) => {
      // app.globalData.imgSrc = obj.url;
      // wx.navigateBack({
      //   delta: -1
      // })
      const src = obj.url;
      const _this = this;
      wx.showLoading({
        title: '上傳中...',
        mask: true,
      });
      wx.uploadFile({
         url:''
      })
    });
  },
  rotate() {
    //在用戶旋轉(zhuǎn)的基礎上旋轉(zhuǎn)90°
    this.cropper.setAngle(this.cropper.data.angle += 90);
  },
  end(e) {
    clearInterval(this.data[e.currentTarget.dataset.type]);
  },
 
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末檐迟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子溶其,更是在濱河造成了極大的恐慌敦间,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厢绝,死亡現(xiàn)場離奇詭異带猴,居然都是意外死亡,警方通過查閱死者的電腦和手機靶病,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門娄周,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沪停,“玉大人,你說我怎么就攤上這事木张。” “怎么了?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵且轨,是天一觀的道長虚婿。 經(jīng)常有香客問我,道長然痊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任锹引,我火速辦了婚禮,結(jié)果婚禮上嫌变,老公的妹妹穿的比我還像新娘。我一直安慰自己东涡,他們只是感情好倘待,可當我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著祖娘,像睡著了一般贞间。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上整以,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天峻仇,我揣著相機與錄音,去河邊找鬼凡蚜。 笑死吭从,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的谱醇。 我是一名探鬼主播步做,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼全度!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起勉盅,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤菇篡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后驱还,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年舆逃,在試婚紗的時候發(fā)現(xiàn)自己被綠了戳粒。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡奄妨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出苹祟,到底是詐尸還是另有隱情砸抛,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布树枫,位于F島的核電站直焙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏砂轻。R本人自食惡果不足惜奔誓,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望搔涝。 院中可真熱鬧厨喂,春花似錦、人聲如沸体谒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至颁褂,卻和暖如春故响,著一層夾襖步出監(jiān)牢的瞬間傀广,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工彩届, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留伪冰,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓樟蠕,卻偏偏與公主長得像贮聂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子寨辩,可洞房花燭夜當晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容