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 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:
|
<<: Use of Linux bzip2 command
>>: Detailed tutorial on how to publish springboot projects through docker plug-in in IDEA
Problem Description Install nginx on Tencent Clou...
When browser vendors bend the standards and take i...
By default, Flash will always be displayed at the ...
Ubuntu 20.04 has been released, bringing many new...
Table of contents Get the time in the past week G...
Table name and fields –1. Student List Student (s...
One time we talked about the dice rolling game. A...
Preface This is an investigation caused by the ex...
First look at the example code: #/bin/bash cal da...
This article uses examples to illustrate the MySQ...
Table of contents 1. The role of watch in vue is ...
This article introduces an example of using HTML+...
To achieve an effect similar to Windows forms, dr...
Scenario 1: Due to server restrictions, only one ...
Table of contents Compare the empty string '&...