vant+vue-cropper-h5實(shí)現(xiàn)圖片上傳前裁剪

要實(shí)現(xiàn)的亞子:
image.png

image.png

image.png
廢話不說,上代碼,先封裝一個(gè)公用組件
<template>
  <div class="main">
    <div class="cropper" style="position: relative;overflow: hidden; text-align: center;width:100%;height:100%;" :style="obj">
      <van-image v-if="show" :src="img" class="img" style="position: absolute;width: 100%;height:100%;left: 0;top: 0;" />
      <van-loading v-else color="#65abff" style="" />
      <!-- option是配置,格式是對(duì)象,getbase64Data是組件的一個(gè)方法獲取裁剪完的頭像 -->
      <h5-cropper @getbase64="getbase64Data" :option="option" @getblob="getBlob" @get-file="getFile"></h5-cropper>
    </div>
  </div>
</template>

<script>
// import { getsign } from "@/api/common";
//import TcVod from 'vod-js-sdk-v6'
export default {
  props: {
    fixedNumber: {
      type: Array,
      default: () => [1, 1]
    },
    image: {
      type: String,
      default: "http://erkong.ybc365.com/36bc0202010231838302739.png"
    },
    radius: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      show: true,
      obj: {
        borderRadius: `${this.radius}px`
      },
      img: this.image,
      option: {
        ceilbutton: true,//操作按鈕是否在頂部    
        fixedNumber: this.fixedNumber,//截圖框的寬高比例    
        canMoveBox: true,//截圖框能否拖動(dòng)  
        fixedBox: false,//固定截圖框大小 不允許改變 
        centerBox: true,//截圖框是否被限制在圖片里面 
        fixed: false,//是否開啟截圖框?qū)捀吖潭ū壤?   
      }
    }
  },
  methods: {
    getbase64Data(data) {
      this.img = data;
    },
    getBlob(blob) {
      // console.log(blob)
      this.$emit('getBlob')
    },
    getFile(file) {
      // console.log(file)
      // this.$emit('getFile')
      // this.show = false
      // this.uploader = this.tcVod.upload({
      //   mediaFile: file,
      // })
      // // 上傳進(jìn)度
      // this.uploader.on('media_progress', (info) => {
      //   console.log('上傳進(jìn)度' + "=>" + info.percent * 100);
      // })
      // // 上傳完成時(shí)
      // this.uploader.on('media_upload', (info) => {
      //   console.log('上傳完成時(shí)' + "=>" + info);
      //   this.$toast.success('上傳成功!')
      // })
      // this.uploader.done().then((doneResult) => {
      //   this.img = (doneResult.video.url);
      //   this.show = true
      this.$emit('getImageUrl', file)
      // })
    }
  },
  // created() {
  //   //獲取簽名
  //   function getSignature() {
  //     return getsign(JSON.stringify({
  //       "Action": "GetUgcUploadSign"
  //     })).then(function(response) {
  //       return response.data.sign
  //     })
  //   };
  //   this.tcVod = new TcVod({
  //     getSignature: getSignature
  //   });
  // },
}
</script>

<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
  ::v-deep .van-loading {
    margin-top: 20%;
  }
}
</style>
然后在其他頁面引入組件,直接用
<!-- 上傳圖片、視頻 -->
        <h1 class="title">上傳圖片:</h1>
        <van-field class="titleImg" readonly input-align="right">
          <template #button>
            <div style="display: flex;">
              <div class="avatar" style="width:80px;height:80px;margin-right:20px;">
                <CropperH5 ref="avatarImage" :fixedNumber="[1,1]" @getImageUrl='getAvatarImageUrl1' :image='avatarImage' />
              </div>
              <div class="avatar" style="width:80px;height:80px;">
                <CropperH5 ref="avatarImage" :fixedNumber="[1,1]" @getImageUrl='getAvatarImageUrl2' :image='avatarImage' />
              </div>
            </div>
          </template>
        </van-field>
data() {
    return {
      avatarImage: "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=319223871,3949806150&fm=26&gp=0.jpg",
      img1: '',
      img2: ''
    }
  },
methods:{
//獲取頭像url
    async getAvatarImageUrl1(file) {
      var formData = new FormData();
      formData.append('file', file); // 固定格式
      const res = await upload(formData)
      if (res) {
        this.img1 = (res.data[0])
        console.log(res);
      }
    },
    async getAvatarImageUrl2(file) {
      var formData = new FormData();
      formData.append('file', file); // 固定格式
      const res = await upload(formData)
      if (res) {
        this.img2 = (res.data[0])
        console.log(res);
      }
    },
}
::v-deep .titleImg {
  padding: 10px 0;
  .van-cell__value {
    display: flex;
  }
  .van-field__control {
    display: none;
  }
  ::v-deep .cropper-crop-box {
    width: 300px;
    height: 300px;
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容