Vue uses plug-ins to cut pictures in proportion

Vue uses plug-ins to cut pictures in proportion

This article shares the specific code of Vue using plug-ins to achieve proportional cutting of pictures for your reference. The specific content is as follows

1. Use plugin - vueCropper

Install the plugin: npm install vue-cropper
Combined with el-element upload component

2. Example:

Main Page

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="Click to change" :src="formData.currentBannerImg" alt="" />
        <i class="el-icon-delete delImg" v-if="formData.currentBannerImg" title="Delete"></i>
        <i v-if="!formData.currentBannerImg" slot="default" class="el-icon-plus"></i>
      </div>
      <div slot="tip" class="el-upload__tip">Only jpg, jpeg, png can be uploaded and the single file cannot exceed 10M</div>
</el-upload>

//Upload the image and crop it after success 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("Please upload images in jpg, jpeg, or png format!", "System prompt", {
          confirmButtonText: "Confirm"
        });
        fileList.splice(fileList.length - 1);
        return;
      }
      // Size limit const isLt2M = file.size / 1024 / 1024 < this.$FileSize;
      if (!isLt2M) {
        this.$alert(`The uploaded file size cannot exceed ${this.$FileSize}MB!`, "System prompt", {
          confirmButtonText: "Confirm"
        });
        fileList.splice(fileList.length - 1);
        return;
      }
      // Add the cropping part this.isShowCropper = true;
      this.$nextTick(() => {
        this.$refs.vueCropperDialog.open(file);
      });
    },

vueCropperDialog is introduced as a component

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

vueCropperDialog.vue

<!-- -->
<template>
  <!-- vueCropper cropping picture implementation -->
  <el-dialog title="Image cropping" :visible.sync="dialogVisible" append-to-body>
    <div class="cropper-content">
      <div class="cropper" style="text-align:center">
        <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')">Rotate left</el-button>
        <el-button icon="el-icon-refresh-right" @click="turnLeftOrRight('right')">Rotate right</el-button>
      </div>
      <div>
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="finish" :loading="loading">Confirm</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      fileinfo:"",
      dialogVisible: false,
      // Basic configuration option for the cropping component
      option: {
        : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
      picsList: [], //Array displayed on the page // Prevent duplicate submission 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;
    },
    //Left rotation turnLeftOrRight(type) {
      if (type == "left") {
        this.$refs.cropper.rotateLeft();
      } else {
        this.$refs.cropper.rotateRight();
      }
    },
    // Click to crop, this step is to get the processed address finish() {
      this.$refs.cropper.getCropBlob((data) => {
        this.loading = true;
        const changeFile = this.blobToFile(data, this.fileinfo);
        const fileUrl = window.URL.createObjectURL(data);
        //Callback after successful cutting this.$emit("cutImgEnter", changeFile, fielUrl);
        this.loading = false;
        this.dialogVisible = false;
      });
    }
  }
};
</script>
<style lang="scss" scoped>
// Screenshot.cropper-content {
  .cropper {
    width: auto;
    height: 300px;
  }
}
.btnBox {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>

3. Effect

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • vue-cropper component realizes image cutting and uploading
  • Vue+element realizes image upload and cropping functions
  • Vue implements mobile image cropping and uploading function
  • Detailed explanation of implementing image cropping function in vue project
  • Mobile image cropping component function based on Vue
  • Cropper js implementation code of image cropping and uploading function based on Vue
  • Vue realizes uploading after cropping pictures
  • Example of cropping pictures with canvas under Vue
  • Implementation of Vue image cropping and uploading component
  • Vue image cropping component example code

<<:  Use of Linux bzip2 command

>>:  Detailed tutorial on how to publish springboot projects through docker plug-in in IDEA

Recommend

Div css naming standards css class naming rules (in line with SEO standards)

There are many tasks to be done in search engine o...

Guide to using env in vue cli

Table of contents Preface Introduction-Official E...

How to completely uninstall Docker Toolbox

Docker Toolbox is a solution for installing Docke...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

React tsx generates random verification code

React tsx generates a random verification code fo...

Detailed explanation of Vue Notepad example

This article example shares the specific code of ...

Basic implementation method of cross-component binding using v-model in Vue

Hello everyone, today we will talk about how to u...

How to implement HTML Table blank cell completion

When I first taught myself web development, there...

Detailed explanation of the usage of grep command in Linux

1. Official Introduction grep is a commonly used ...

Analysis of MySQL concurrency issues and solutions

Table of contents 1. Background 2. Slow query cau...