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

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...

Mysql Sql statement exercises (50 questions)

Table name and fields –1. Student List Student (s...

From CSS 3D to spatial coordinate axis with source code

One time we talked about the dice rolling game. A...

NodeJs high memory usage troubleshooting actual combat record

Preface This is an investigation caused by the ex...

Detailed explanation of daily_routine example code in Linux

First look at the example code: #/bin/bash cal da...

Analysis of MySQL lock mechanism and usage

This article uses examples to illustrate the MySQ...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Nginx domain forwarding usage scenario code example

Scenario 1: Due to server restrictions, only one ...

Why is it not recommended to use an empty string as a className in Vue?

Table of contents Compare the empty string '&...