Vue结合cropper.js实现图片裁剪与上传功能详解

作者:山岚时间:2026-10-23 03:00:23浏览:237

本文介绍如何在Vue项目中集成cropper.js库,实现图片裁剪与上传功能。通过npm安装依赖,在组件中引入库,配置HTML模板与JS逻辑,包括初始化裁剪框、处理文件选择、生成URL及提交上传。代码涵盖数据绑定、事件监听及样式调整,提供完整实现方案。

Vue结合cropper.js实现图片裁剪与上传功能详解

此前完成的一个Vue项目涉及头像裁剪与上传功能,参考了相关技术文章并进行了修改完善,现分享具体实现过程。原文参考:https://www.jb51.net/article/135719.htm

引入cropper.js库

第一步是下载并引入cropper.js库。


npm install cropper js --save

在目标页面中引入该库:import Cropper from "cropper js"

HTML结构配置

HTML结构代码如下:


<template> 
 <div id="demo"> 
 <!-- 遮罩层 --> 
 <div class="container" v-show="panel"> 
 <div> 
  <img id="image" :src="url" alt="Picture"> 
 </div> 
 <button type="button" id="button" @click="commit">确定</button> 
 <button type="button"id="cancel" @click="cancel">取消</button> 
 </div> 
 <div style="padding:20px;"> 
 <div class="show"> 
  <div class="picture" :style="'backgroundImage:url('+headerImage+')'"> 
  </div> 
 </div> 
 <div style="margin-top:20px;text-align: left;"> 
  <input type="file" id="change" accept="image" @change="change"> 
  <label for="change"></label> 
 </div> 
 </div> 
 </div> 
</template> 

JavaScript核心逻辑

核心逻辑主要依赖JavaScript代码,具体如下:

1. 在data中定义初始变量并绑定数据,其中imgCropperData用于判断图片格式。


data() { 
 return { 
 headerImage: "", 
 picValue: "", 
 cropper: "", 
 croppable: false, 
 panel: false, 
 url: "", 
 imgCropperData: { 
 accept: "image/gif, image/jpeg, image/png, image/jpg" 
 } 
 }; 
 } 

2. 在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. methods中包含多个方法,如创建URL路径、处理input框change事件、canvas绘图、确定提交上传,以及取消事件函数,同时判断上传文件的类型和大小。


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("上传成功"); 
 //这边写图片的上传 
 } 
 } 

样式与总结

CSS样式代码未在此列出,可从GitHub仓库https://github.com/leileibrother/cropper-js-vue-获取。

总结

以上介绍了基于Vue和cropper.js的图片裁剪上传功能实现,希望有所帮助。如有疑问请留言,小编会及时回复。感谢支持!

作者最新文章
相关文章
更多