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

vue pc端项目el-upload上传图片时加水印

html代码:

<a-uploadclass="avatar-uploader"list-type="picture-card":file-list="uploadFileList":custom-request="uploadDoneHandle":before-upload="beforeUpload":remove="removeHandle"v-decorator="['path', { rules: formValidateRules.path }]"@preview="previewHandle"@change="uploadChangeHandle"
><div v-if="uploadFileList.length < 1"><a-icon :type="uploadLoading ? 'loading' : 'plus'" /></div>
</a-upload>

画布这时就需要在beforeUploadHandle这个方法中去生成水印,然后通过后端上传接口,把图片传给后端,然后再接收后端返回的数据
下面是beforeUploadHandle方法

 beforeUpload (file) {return new Promise(resolve => {const reader = new FileReader()reader.readAsDataURL(file) // file转base64reader.onload = e => {const canvas = document.createElement('canvas')const img = new Image()img.src = e.target.resultimg.onload = () => {//图片加载后再添加水印,否则可能报错let width = img.widthlet height = img.heightcanvas.width = widthcanvas.height = heightconst ctx = canvas.getContext('2d') // 绘制2d图形ctx.drawImage(img, 0, 0, width, height)var basePx = canvas.width//字体大小  照片添加水印var fontSize = basePx / 20 //水印文字尺寸ctx.shadowColor = 'rgba(0, 0, 0,1)'ctx.shadowOffsetX = 3 //水印文字阴影ctx.shadowOffsetY = 3ctx.shadowBlur = 5ctx.rotate((-20 * Math.PI) / 180) //水印文字倾斜ctx.font = fontSize + 'px 微软雅黑'ctx.fillStyle = 'rgba(255,255,255,0.6)' //水印透明度var watermark =sessionStorage.getItem('id') +'|' +sessionStorage.getItem('name') +'|' +sessionStorage.getItem('orgName') //水印文字内容var watermarkSplit = watermark.split('|')let w = 1 //横向绘制次数let h = 4 //纵向绘制次数'//水印的总绘制次数let num = Math.round(w * h)for (let i = 0; i < num; i++) {if (i < w) {ctx.fillText(watermarkSplit[0],(width / 3) * i,height / 2 - 2.7 * fontSize)ctx.fillText(watermarkSplit[1],(width / 3) * i,height / 2 - 1.5 * fontSize)ctx.fillText(watermarkSplit[2],(width / 3) * i,height / 2 - 0.5 * fontSize)} else if (i >= w && i < w * 2) {ctx.fillText(watermarkSplit[0],(width / 3) * (i - w),height - 2.7 * fontSize)ctx.fillText(watermarkSplit[1],(width / 3) * (i - w),height - 1.5 * fontSize)ctx.fillText(watermarkSplit[2],(width / 3) * (i - w),height - 0.5 * fontSize)} else if (i >= w * 2 && i < w * 3) {ctx.fillText(watermarkSplit[0],(width / 3) * (i - w * 2),height * 1.5 - 2.7 * fontSize)ctx.fillText(watermarkSplit[1],(width / 3) * (i - w * 2),height * 1.5 - 1.5 * fontSize)ctx.fillText(watermarkSplit[2],(width / 3) * (i - w * 2),height * 1.5 - 0.5 * fontSize)}}var dataBase64 = canvas.toDataURL(file.type) // 输出压缩后的base64// base64转fileconst arr = dataBase64.split(',')const mime = arr[0].match(/:(.*?);/)[1]const bstr = atob(arr[1])let n = bstr.lengthconst u8arr = new Uint8Array(n)while (n--) {u8arr[n] = bstr.charCodeAt(n)}const files = new File([new Blob([u8arr], { type: mime })],file.name,{ type: file.type })files.uid = file.uidresolve(files)}}})/* const { result } = fileCheckForImage(file)return result */},
http://www.lryc.cn/news/122242.html

相关文章:

  • ubuntu中redis+mysql安装使用
  • 若依-plus-vue启动显示Redis连接错误
  • 【C++11智能指针】
  • 如何实现Excel中多级数据联动
  • 使用Pytest集成Allure生成漂亮的图形测试报告
  • opencv基础49-图像轮廓02-矩特征cv2.moments()->(形状分析、物体检测、图像识别、匹配)
  • 什么是CSS Grid布局?什么是Flexbox布局?它们两者有什么不同?
  • Centos中pip install mysqlclient失败
  • arcgis更改图层字段名脚本
  • Android 13 MTK平台添加自定义按键,以及CTS问题解决
  • 深入了解 Postman Test 校验的使用方法
  • 岛屿的最大面积(力扣)递归 JAVA
  • MySQL入门学习教程(一)
  • 【CTF-web】修改请求头(XFF)
  • springboot mongodb 配置多数据源
  • Python3 安装、环境变量配置、PyCharm新建Python项目
  • 用python来爬取某鱼的商品信息(2/2)
  • 【Fegin技术专题】「原生态」打开Fegin之RPC技术的开端,你会使用原生态的Fegin吗?(上)
  • React Native Vector Icons的使用
  • Redis安装和配置(Linux)
  • 安卓源码分析(10)Lifecycle实现组件生命周期管理
  • IP 多播协议(IP Multicast Protocol)
  • Jmeter 配置环境变量,简明教程专享
  • WebService—XFire配置笔记
  • 【LangChain学习】基于PDF文档构建问答知识库(一)前期准备
  • Word(1):文章页码设置
  • Linux MQTT智能家居(温度,湿度,环境监测,摄像头等界面布局设置)
  • 权衡与选择:如何巧妙管理项目需求的优先级
  • UGUI组件EventTrigger用法
  • Visual Studio 2019 详细安装教程(图文版)