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

Detailed explanation of the basic commands of Firewalld firewall in Centos7

1. Basics of Linux Firewall The Linux firewall sy...

Summary of three ways to create new elements

First: via text/HTML var txt1="<h1>Tex...

MySQL 5.7.18 installation tutorial and problem summary

MySQL 5.7.18 installation and problem summary. I ...

How to implement parent-child component communication with Vue

Table of contents 1. Relationship between parent ...

Nginx access log and error log parameter description

illustrate: There are two main types of nginx log...

How to set focus on HTML elements

Copy code The code is as follows: <body <fo...

The difference and usage between div and span

Table of contents 1. Differences and characterist...

About converting textarea text to html, that is, carriage return and line break

Description: Change the carriage return in the tex...

Detailed explanation of three ways to wrap text in el-table header

Table of contents Problem Description Rendering T...

Use of Linux passwd command

1. Command Introduction The passwd command is use...

Solve the problem that the docker container cannot ping the external network

Today, when I was building a redis environment in...

Vue project packaging and optimization implementation steps

Table of contents Packaging, launching and optimi...