Detailed explanation of webpage screenshot function in Vue

Detailed explanation of webpage screenshot function in Vue

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)
This is because the backend receives the data as file type, so I need to convert the blob into file and then upload the file using formData.

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:
  • Do you know how to use Vue to take screenshots of web pages?

<<:  border-radius method to add rounded borders to elements

>>:  Several skills you must know when making web pages

Recommend

How to handle images in Vue forms

question: I have a form in Vue for uploading blog...

Summary of MySQL stored procedure permission issues

MySQL stored procedures, yes, look like very rare...

Solution to 1045 error when navicat connects to mysql

When connecting to the local database, navicat fo...

Analysis of MySQL example DTID master-slave principle

Table of contents 1. Basic Concepts of GTID 2. GT...

React example of how to get the value of the input box

React multiple ways to get the value of the input...

Docker renames the image name and TAG operation

When using docker images, images with both REPOSI...

How to make JavaScript sleep or wait

Table of contents Overview Checking setTimeout() ...

Tomcat+Mysql high concurrency configuration optimization explanation

1.Tomcat Optimization Configuration (1) Change To...

Using JavaScript difference to implement a comparison tool

Preface At work, I need to count the materials su...

Writing tab effects with JS

This article example shares the specific code for...

How to install rabbitmq-server using yum on centos

Socat needs to be installed before installing rab...