溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue如何利用插件實現按比例切割圖片

發布時間:2021-09-24 10:51:02 來源:億速云 閱讀:172 作者:小新 欄目:開發技術

這篇文章給大家分享的是有關vue如何利用插件實現按比例切割圖片的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1.使用插件——vueCropper

安裝該插件:npm install vue-cropper
結合el-element的上傳組件

2.示例:

主頁面

data(){
 return {
     formData:{
        currentBannerImg:""
     },
     isShowCropper :false,
 }
}
<el-upload
      class="avatar-uploader"
      list-type="picture-card"
      action=""
      accept=".jpg, .jpeg, .png"
      :with-credentials="true"
      :on-change="fileChangeBanner"
      :auto-upload="false"
      :show-file-list="false"
    >
    <div class="imgBoX">
        <img class="bannerImg" v-if="formData.currentBannerImg" title="點擊更換" :src="formData.currentBannerImg" alt="" />
        <i class="el-icon-delete delImg" v-if="formData.currentBannerImg" title="刪除"></i>
        <i v-if="!formData.currentBannerImg" slot="default" class="el-icon-plus"></i>
      </div>
      <div slot="tip" class="el-upload__tip">只能上傳jpg、jpeg、png且單個文件不超過10M</div>
</el-upload>

// 上傳圖片,成功后調裁剪
    async fileChangeBanner(file, fileList) {
      const fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
      const fileTypeArr = ["jpg", "jpeg", "png", "JPG", "JPEG", "PNG"];
      if (fileTypeArr.indexOf(fileType) < 0) {
        this.$alert("請上傳格式為jpg、jpeg、png的圖片!", "系統提示", {
          confirmButtonText: "確定"
        });
        fileList.splice(fileList.length - 1);
        return;
      }
      // 大小限制
      const isLt2M = file.size / 1024 / 1024 < this.$FileSize;
      if (!isLt2M) {
        this.$alert(`上傳文件大小不能超過 ${this.$FileSize}MB!`, "系統提示", {
          confirmButtonText: "確定"
        });
        fileList.splice(fileList.length - 1);
        return;
      }
      // 添加裁剪部分
      this.isShowCropper = true;
      this.$nextTick(() => {
        this.$refs.vueCropperDialog.open(file);
      });
    },

vueCropperDialog作為組件被引入

<vueCropperDialog @cutImgEnter="cutImgEnter" v-if="isShowCropper" ref="vueCropperDialog"></vueCropperDialog>

vueCropperDialog.vue

<!--  -->
<template>
  <!-- vueCropper 剪裁圖片實現-->
  <el-dialog title="圖片剪裁" :visible.sync="dialogVisible" append-to-body>
    <div class="cropper-content">
      <div class="cropper" >
        <vueCropper
          ref="cropper"
          :img="option.img"
          :outputSize="option.size"
          :outputType="option.outputType"
          :info="true"
          :full="option.full"
          :canMove="option.canMove"
          :canMoveBox="option.canMoveBox"
          :original="option.original"
          :autoCrop="option.autoCrop"
          :fixed="option.fixed"
          :fixedNumber="option.fixedNumber"
          :centerBox="option.centerBox"
          :infoTrue="option.infoTrue"
          :fixedBox="option.fixedBox"
        ></vueCropper>
      </div>
    </div>
    <div slot="footer" class="dialog-footer btnBox">
      <div>
        <el-button icon="el-icon-refresh-left" @click="turnLeftOrRight('left')">左旋轉</el-button>
        <el-button icon="el-icon-refresh-right" @click="turnLeftOrRight('right')">右旋轉</el-button>
      </div>
      <div>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="finish" :loading="loading">確認</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      fileinfo:"",
      dialogVisible: false,
      // 裁剪組件的基礎配置option
      option: {
        img: "", // 裁剪圖片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 0.8, // 裁剪生成圖片的質量
        outputType: "jpeg", // 裁剪生成圖片的格式
        canScale: false, // 圖片是否允許滾輪縮放
        autoCrop: true, // 是否默認生成截圖框
        // autoCropWidth: 563, // 默認生成截圖框寬度
        // autoCropHeight: 387, // 默認生成截圖框高度
        fixedBox: true, // 固定截圖框大小 不允許改變
        fixed: true, // 是否開啟截圖框寬高固定比例
        fixedNumber: [1.45, 1], // 截圖框的寬高比例
        full: true, // 是否輸出原圖比例的截圖
        canMoveBox: false, // 截圖框能否拖動
        original: false, // 上傳圖片按照原始比例渲染
        centerBox: false, // 截圖框是否被限制在圖片里面
        infoTrue: true // true 為展示真實輸出圖片寬高 false 展示看到的截圖框寬高
      },
      picsList: [], //頁面顯示的數組
      // 防止重復提交
      loading: false
    };
  },
  methods: {
    open(file) {
      this.fileinfo = file;
      this.option.img = file.url;
      this.dialogVisible = true;
    },
    blobToFile(theBlob, file) {
      const files = new window.File([theBlob], file.name, { type: theBlob.type });
      return files;
    },
    //左旋轉
    turnLeftOrRight(type) {
      if (type == "left") {
        this.$refs.cropper.rotateLeft();
      } else {
        this.$refs.cropper.rotateRight();
      }
    },
    // 點擊裁剪,這一步是可以拿到處理后的地址
    finish() {
      this.$refs.cropper.getCropBlob((data) => {
        this.loading = true;
        const changeFile = this.blobToFile(data, this.fileinfo);
        const fielUrl = window.URL.createObjectURL(data);
        //裁剪成功后的回調
        this.$emit("cutImgEnter", changeFile, fielUrl);
        this.loading = false;
        this.dialogVisible = false;
      });
    }
  }
};
</script>
<style lang="scss" scoped>
// 截圖
.cropper-content {
  .cropper {
    width: auto;
    height: 300px;
  }
}
.btnBox {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>

感謝各位的閱讀!關于“vue如何利用插件實現按比例切割圖片”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女