当前位置: 首页 > news >正文

vue+elementui+vueCropper裁剪上传图片背景颜色为黑色解决方案

组件
<template><div class="avatar-uploader"><!-- 头像展示 --><div class="avatar-show"><img :src="avatarUrl" v-if="avatarUrl" class="avatar"><i class="el-icon-plus avatar-uploader-icon" v-else></i></div><!-- 上传按钮 --><el-uploadclass="avatar-upload"action="#":show-file-list="false":before-upload="beforeAvatarUpload":http-request="handleUpload"><el-button size="small" type="primary">点击上传</el-button><div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过2MB</div></el-upload><!-- 裁剪对话框 --><el-dialog title="裁剪头像" :visible.sync="dialogVisible" width="800px"><div class="cropper-content"><div class="cropper"><vue-cropperref="cropper":img="cropperImg":output-size="1":output-type="'png'":info="true":full="true":can-move="true":can-move-box="true":fixed="true":fixed-number="[1, 1]":center-box="true":auto-crop="true":auto-crop-width="200":auto-crop-height="200"></vue-cropper></div></div><span slot="footer" class="dialog-footer"><el-button @click="dialogVisible = false">取 消</el-button><el-button type="primary" @click="handleCrop">确 定</el-button></span></el-dialog></div>
</template><script>
import { VueCropper } from 'vue-cropper'export default {components: {VueCropper},props: {value: String // 用于v-model绑定},data() {return {dialogVisible: false,cropperImg: '', // 裁剪图片的base64// avatarUrl: this.value || require('@/assets/logo.png') // 默认头像avatarUrl: null // 默认头像}},methods: {// 上传前校验beforeAvatarUpload(file) {const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'const isLt2M = file.size / 1024 / 1024 < 2if (!isJPG) {this.$message.error('上传头像图片只能是 JPG/PNG 格式!')}if (!isLt2M) {this.$message.error('上传头像图片大小不能超过 2MB!')}return isJPG && isLt2M},// 处理上传handleUpload(file) {const reader = new FileReader()reader.onload = (event) => {this.cropperImg = event.target.resultthis.dialogVisible = truethis.$nextTick(() => {this.$refs.cropper.replace(event.target.result)})}reader.readAsDataURL(file.file)},// 处理裁剪handleCrop() {this.$refs.cropper.getCropBlob((blob) => {// 创建FormData对象上传const formData = new FormData()formData.append('file', blob, 'avatar.png')// 这里替换为你的上传APIthis.$axios.post('/api/upload/avatar', formData, {headers: {'Content-Type': 'multipart/form-data'}}).then(response => {this.avatarUrl = response.data.urlthis.$emit('input', this.avatarUrl) // 更新v-modelthis.dialogVisible = falsethis.$message.success('头像上传成功!')}).catch(error => {this.$message.error('头像上传失败!')console.error(error)})})},// 也可以使用base64格式handleCropBase64() {this.$refs.cropper.getCropData((data) => {this.avatarUrl = datathis.$emit('input', data) // 更新v-modelthis.dialogVisible = falsethis.$message.success('头像裁剪成功!')})}},watch: {value(newVal) {this.avatarUrl = newVal}}
}
</script><style scoped>
.avatar-uploader {display: flex;flex-direction: column;align-items: center;
}.avatar-show {width: 150px;height: 150px;border: 1px dashed #d9d9d9;border-radius: 50%;overflow: hidden;display: flex;justify-content: center;align-items: center;margin-bottom: 20px;
}.avatar {width: 100%;height: 100%;object-fit: cover;
}.avatar-uploader-icon {font-size: 28px;color: #8c939d;
}.cropper-content {display: flex;justify-content: center;
}.cropper {width: 500px;height: 500px;
}.el-upload__tip {margin-top: 10px;text-align: center;
}
</style>

组件调用

<template>
<div>
<h2>个人头像设置</h2>
<avatar-uploader v-model="avatar"></avatar-uploader>
</div>
</template>

<script>
import AvatarUploader from '@/components/AvatarUploader'

export default {
components: {
AvatarUploader
},
data() {
return {
avatar: '' // 初始头像地址
}
}
}
</script>

注意一定要配置:output-type="'png'"

http://www.dtcms.com/a/306871.html

相关文章:

  • ‌我的第一个开源项目:跃动的心
  • 物流分拣漏检率↓78%!陌讯动态光流算法在包裹移动识别的技术突破
  • GCC链接技术深度解析:性能与空间优化
  • [mcp: McpSchema]-源码分析
  • 第1课:向量与矩阵运算
  • 搭建实时足球比分系统从零到一的实战指南
  • Day 4-1: 机器学习算法全面总结
  • 全新AI工具小程序源码 全开源(源码下载)
  • 深入浅出:在 Spring Boot 中构建实时应用 - 全面掌握 WebSocket
  • 解决 Docker 报错 “exec: no such file or directory”
  • 文件权限值的表示方法
  • PHP/Java/Python实现:如何有效防止恶意文件上传
  • Go 语言make函数
  • 输电线路绝缘子泄漏电流在线监测装置的技术解析与应用价值
  • Python读取获取波形图波谷/波峰
  • Directory Opus 使用优化
  • 30道JS高频经典笔试题集合+详解(一)
  • 视觉系统引导冲床冲压:工业自动化的“智能之眼”
  • Dify 从入门到精通(第 4/100 篇):快速上手 Dify 云端:5 分钟创建第一个应用
  • AI培训项目《人工智能大模型应用工程师》课程学习大纲分享!
  • 【sklearn(01)】数据集加载、划分,csv文件创建,特征工程,无量纲化
  • 【编号65】广西地理基础数据(道路、水系、四级行政边界、地级城市、DEM等)
  • 我的世界模组开发教程——资源(1)
  • JeecgBoot(1):前后台环境搭建
  • C#_创建自己的MyList列表
  • 汽车电子控制系统开发的整体安全理念
  • SOA增益谱与ASE光谱的区别
  • SSRF漏洞基础
  • RESTful API和WebSocket的优缺点,各自适合以及不适合什么样的场景
  • LLM 模型部署难题的技术突破:从轻量化到分布式推理的全栈解决方案