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

html代码:

html 复制代码
<a-upload
    class="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方法

javascript 复制代码
 beforeUpload (file) {
      return new Promise(resolve => {
        const reader = new FileReader()
        reader.readAsDataURL(file) // file转base64
        reader.onload = e => {
          const canvas = document.createElement('canvas')
          const img = new Image()
          img.src = e.target.result
          img.onload = () => {
            //图片加载后再添加水印,否则可能报错
            let width = img.width
            let height = img.height
            canvas.width = width
            canvas.height = height
            const 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 = 3
            ctx.shadowBlur = 5
            ctx.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转file
            const arr = dataBase64.split(',')
            const mime = arr[0].match(/:(.*?);/)[1]
            const bstr = atob(arr[1])
            let n = bstr.length
            const 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.uid
            resolve(files)
          }
        }
      })
      /* const { result } = fileCheckForImage(file)
            return result */
    },
相关推荐
哪 吒1 分钟前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
安冬的码畜日常9 分钟前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
杨荧27 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
l1x1n036 分钟前
No.3 笔记 | Web安全基础:Web1.0 - 3.0 发展史
前端·http·html
Q_w774241 分钟前
一个真实可用的登录界面!
javascript·mysql·php·html5·网站登录
昨天;明天。今天。1 小时前
案例-任务清单
前端·javascript·css
一丝晨光1 小时前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
Front思1 小时前
vue使用高德地图
javascript·vue.js·ecmascript
zqx_72 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
惜.己2 小时前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5