Recently, there is a requirement for uploading pictures in the project, but the pictures uploaded by customers are of different sizes, so we need to stipulate the proportion of the customer's pictures, but it needs to be what the customer needs, so I thought of taking screenshots. Achieve results Our architecture is vue, so we use a vue screenshot plug-in Install the plugin: npm install vue-cropper --save-dev Importing components import Vue from 'vue'; import { VueCropper } from "vue-cropper"; Vue.use(VueCropper) Core code <div> <span class="spanStyle">Product images:</span> <!--The entire image preview div--> <div style="display:flex;padding-left: 300px;"> <div class="info-item" style="flex:1;margin-left:-160px;margin-top: -25px"> <label class="btn btn-orange theme-bg-gray theme-white w90" for="uploads" style="display:inline-block;width: 70px;padding: 0;text-align: center;line-height: 28px;" >Select image</label> <input type="file" id="uploads" :value="imgFile" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event, 1)"> <div class="line" style="margin-left: -280px;margin-top: 85px;"> <div class="cropper-content" style="margin-top:-60px;margin-left:160px;"> <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" :autoCropWidth="option.autoCropWidth" :autoCropHeight="option.autoCropHeight" :fixedBox="option.fixedBox" @realTime="realTime" ></vueCropper> </div> <!--The captured image is displayed in div--> <div style="margin-left:700px;"> <div class="show-preview" :style="{'width': '300px', 'height':'150px', 'overflow': 'hidden', 'margin-left': '-650px'}"> <div :style="previews.div" > <img :src="previews.url" :style="previews.img"> </div> </div> </div> </div> </div> </div> </div> </div> Preview method data (){ return { headImg:'', //Cut the image and upload crap: false, previews: {}, option: { img: '', outputSize:1, //Image quality after cutting (0.1-1) full: false, // Output original image scale screenshot props name full outputType: 'png', canMove: true, original: false, canMoveBox: true, autoCrop: true, autoCropWidth: 300, autoCropHeight: 150, fixedBox: true }, fileName:'', //Local file address downImg: '#', imgFile:'', uploadImgRelaPath:'', //The address of the uploaded image (without the server domain name) } }, methods:{ // Real-time preview function realTime(data) { console.log('realTime') this.previews = data }, //Select a local image uploadImg(e, num) { console.log('uploadImg'); var _this = this; //Upload picture var file = e.target.files[0] _this.fileName = file.name; if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) { alert('The image type must be one of .gif, jpeg, jpg, png, bmp') return false } var reader = new FileReader(); reader.onload = (e) => { let data; if (typeof e.target.result === 'object') { // Convert Array Buffer to blob. If it is base64, it is not needed. data = window.URL.createObjectURL(new Blob([e.target.result])) } else { data = e.target.result } if (num === 1) { _this.option.img = data } else if (num === 2) { _this.example2.img = data } } // // Convert to blob reader.readAsArrayBuffer(file); } } Then we need to get the image after the capture, and we use the callback function of Cropper to get its image. Here we get the image that we converted to blob format (it cannot be displayed without conversion) productAdd(){ this.$refs.cropper.getCropBlob((data) => { //This data is the blob image we intercepted let formData = new FormData(); //Get the file name, because you can't use this in the file conversion. So you need to use a variable to assign var name=this.fileName var file = new File([data], name, {type: data.type, lastModified: Date.now()}); formData.append("files",file) new Promise((resolve, reject) => { productAdd(formData).then(response => { if (response.code == 20000) { Dialog.alert({ title: 'Tips', message: 'Saved successfully! ' }).then(() => { this.back('/productInfo') }); } }).catch(error => { reject(error) }) }) }) } It still needs to be modified when actually applied to your own project, such as editing pictures and echoing them, uploading multiple cut pictures, etc. The above is the detailed content of the detailed explanation of Vue's implementation of web page screenshot function. For more information about Vue web page screenshot, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: border-radius method to add rounded borders to elements
>>: Several skills you must know when making web pages
question: I have a form in Vue for uploading blog...
MySQL stored procedures, yes, look like very rare...
When connecting to the local database, navicat fo...
Table of contents 1. Basic Concepts of GTID 2. GT...
React multiple ways to get the value of the input...
The MySQL built on Tencent Cloud is always very s...
When using docker images, images with both REPOSI...
Table of contents Overview Checking setTimeout() ...
1.Tomcat Optimization Configuration (1) Change To...
Table of contents 1. Dockerfile 2. pom configurat...
Table of contents Generate random numbers Generat...
Preface At work, I need to count the materials su...
Template 1: login.vue <template> <p clas...
This article example shares the specific code for...
Socat needs to be installed before installing rab...