cropper js implements image cropping and uploading function
This article mainly introduces to you the relevant information about the image cropping and uploading function of cropper js based on vue. I hope it can help you.
First download and import cropper js,
npm install cropper js --save
Introduce it on the required page: import Cropper from "cropper js"
html is as follows:
<template> <p id="demo"> <!-- 遮罩层 --> <p class="container" v-show="panel"> <p> <img id="image" :src="url" alt="Picture"> </p> <button type="button" id="button" @click="commit">确定</button> <button type="button"id="cancel" @click="cancel">取消</button> </p> <p style="padding:20px;"> <p class="show"> <p class="picture" :style="'backgroundImage:url('+headerImage+')'"> </p> </p> <p style="margin-top:20px;text-align: left;"> <input type="file" id="change" accept="image" @change="change"> <label for="change"></label> </p> </p> </p> </template>
Mainly js code, as follows
1, set the initial variables in data, bind the data, imgCropperData is I defined it to determine the image format.
data() { return { headerImage: "", picValue: "", cropper: "", croppable: false, panel: false, url: "", imgCropperData: { accept: "image/gif, image/jpeg, image/png, image/jpg" } }; }
2, initially change the cropping frame in mounted
mounted() { //初始化这个裁剪框 var self = this; var image = document.getElementById("image"); this.cropper = new Cropper(image, { aspectRatio: 1, viewMode: 1, background: false, zoomable: false, ready: function() { self.croppable = true; } }); }
3. There are many methods, including Create URL path, input box change event, canvas drawing, confirm submission and upload. I also added a cancellation event function to determine the type and size of the uploaded file.
methods: { //取消上传 cancel() { this.panel = false; var obj = document.getElementById('change') ; obj.outerHTML=obj.outerHTML; }, //创建url路径 getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { // basic url = window.createObjectURL(file); } else if (window.URL != undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file); } else if (window.webkitURL != undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file); } return url; }, //input框change事件,获取到上传的文件 change(e) { let files = e.target.files || e.dataTransfer.files; if (!files.length) return; let type = files[0].type; //文件的类型,判断是否是图片 let size = files[0].size; //文件的大小,判断图片的大小 if (this.imgCropperData.accept.indexOf(type) == -1) { alert("请选择我们支持的图片格式!"); return false; } if (size > 5242880) { alert("请选择5M以内的图片!"); return false; } this.picValue = files[0]; this.url = this.getObjectURL(this.picValue); //每次替换图片要重新得到新的url if (this.cropper) { this.cropper.replace(this.url); } this.panel = true; }, //确定提交 commit() { this.panel = false; var croppedCanvas; var roundedCanvas; if (!this.croppable) { return; } // Crop croppedCanvas = this.cropper.getCroppedCanvas(); // Round roundedCanvas = this.getRoundedCanvas(croppedCanvas); this.headerImage = roundedCanvas.toDataURL(); //上传图片 this.postImg(); }, //canvas画图 getRoundedCanvas(sourceCanvas) { var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); var width = sourceCanvas.width; var height = sourceCanvas.height; canvas.width = width; canvas.height = height; context.imageSmoothingEnabled = true; context.drawImage(sourceCanvas, 0, 0, width, height); context.globalCompositeOperation = "destination-in"; context.beginPath(); context.arc( width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true ); context.fill(); return canvas; }, //提交上传函数 postImg() { alert("上传成功"); //这边写图片的上传 } }
Related recommendations:
How to implement the front-end cropping and uploading image function
The above is the detailed content of cropper js implements image cropping and uploading function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

1. Open Kugou Music and click on your profile picture. 2. Click the settings icon in the upper right corner. 3. Click [Upload Music Works]. 4. Click [Upload Works]. 5. Select the song and click [Next]. 6. Finally, click [Upload].

With the advent of the digital age, music platforms have become one of the main ways for people to obtain music. However, sometimes when we listen to songs, we find that there are no lyrics, which is very disturbing. Many people hope that lyrics can be displayed when listening to songs to better understand the content and emotions of the songs. QQ Music, as one of the largest music platforms in China, also provides users with the function of uploading lyrics, so that users can better enjoy music and feel the connotation of the songs. The following will introduce how to upload lyrics on QQ Music. first

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Upload speed becomes very slow? I believe this is a problem that many friends will encounter when uploading things on their computers. If the network is unstable when using a computer to transfer files, the upload speed will be very slow. So how can I increase the network upload speed? Below, the editor will tell you how to solve the problem of slow computer upload speed. When it comes to network speed, we all know that the speed of opening web pages, download speed, and upload speed are also very critical. Especially some users often need to upload files to the network disk, so a fast upload speed will undoubtedly save you a lot of money. Less time, what should I do if the upload speed is slow? Below, the editor brings you pictures and texts on how to deal with slow computer upload speeds. How to solve the problem of slow computer upload speed? Click "Start--Run" or "Window key"

As long as the computer is equipped with a camera, it can take pictures, but some users still don't know how to take pictures and upload them. Now I will give you a detailed introduction to the method of taking pictures on the computer, so that users can upload the pictures wherever they want. How to take photos and upload them on a computer 1. Mac computer 1. Open Finder and click the application on the left. 2. After opening, click the Camera application. 3. Just click the photo button below. 2. Windows computer 1. Open the search box below and enter camera. 2. Then open the searched application. 3. Click the photo button next to it.
