canvas实现给照片添加水印

1.使用input选择照片

<canvas id="photo"></canvas>
<input type="file" @change="selectPhoto">

3.水印配置

wartermarkOptions: {
    text: '章某人',
    color: 'blue',
    angle: -Math.PI / 18,
    fontSize: 20,
    cols: 4,
    rows: 3
}

3.拿到file转为base64,然后创建一个新的canvas用来画水印,最后将水印canvas和照片合并成一个新的canvas

(ps:同样适用与网络图片)

selectPhoto(e) {
            let { canvas: cvs, ctx, wartermarkOptions } = this;
            let { text, color, angle, fontSize, cols, rows } = wartermarkOptions;
            let file = e.target.files[0];
            const reader = new FileReader();
            reader.onload = function (e) {
                const base64String = e.target.result;
                let { width: c_w, height: c_h } = cvs;
                let img = new Image();
                img.src = base64String;
                img.onload = () => {
                    ctx.drawImage(img, 0, 0, c_w, c_h);
                    const watermarkText = text;
                    const tempCanvas = document.createElement('canvas');
                    const tempCtx = tempCanvas.getContext('2d');
                    tempCanvas.width = c_w;
                    tempCanvas.height = c_h;
                    tempCtx.font = `${fontSize}px sans-serif`;
                    tempCtx.fillStyle = color;
                    tempCtx.textAlign = 'center';
                    tempCtx.textBaseline = 'middle';

                    const xInterval = (c_w - (text.length * fontSize) * rows) / (rows - 1);
                    const yInterval = (c_h - fontSize * cols) / (cols - 1);

                    // 添加三行三列水印
                    for (let i = 0; i < rows; i++) {
                        for (let j = 0; j < cols; j++) {
                            let x = i * xInterval + (i * (text.length * fontSize)) + (text.length * fontSize) / 2;
                            let y = j * yInterval + (j * fontSize) + fontSize / 2;
                            tempCtx.fillText(watermarkText, x, y);
                        }
                    }

                    ctx.save();
                    ctx.translate(c_w / 2, c_h / 2);
                    ctx.rotate(angle);
                    ctx.drawImage(tempCanvas, -c_w / 2, -c_h / 2);
                    ctx.restore();

                    //添加完水印的照片
                    // let warterPhotoBase64 = cvs.toDataURL('image/png');
                    // console.log('添加完水印的照片', warterPhotoBase64)
                };
            };
            reader.readAsDataURL(file);
        }

4.完整代码 

<template>
    <div>
        <canvas id="photo"></canvas>
        <input type="file" @change="selectPhoto">
    </div>
</template>

<script>
export default {
    name: 'watermarkPhoto',
    data() {
        return {
            canvas: null,
            ctx: null,
            wartermarkOptions: {
                text: '章某人',
                color: 'blue',
                angle: -Math.PI / 18,
                fontSize: 20,
                cols: 4,
                rows: 3
            }
        };
    },
    mounted() {
        this.canvas = document.getElementById('photo');
        this.ctx = this.canvas.getContext('2d');
        this.canvas.width = 300;
        this.canvas.height = 400;
    },
    methods: {
        selectPhoto(e) {
            let { canvas: cvs, ctx, wartermarkOptions } = this;
            let { text, color, angle, fontSize, cols, rows } = wartermarkOptions;
            let file = e.target.files[0];
            const reader = new FileReader();
            reader.onload = function (e) {
                const base64String = e.target.result;
                let { width: c_w, height: c_h } = cvs;
                let img = new Image();
                img.src = base64String;
                img.onload = () => {
                    ctx.drawImage(img, 0, 0, c_w, c_h);
                    const watermarkText = text;
                    const tempCanvas = document.createElement('canvas');
                    const tempCtx = tempCanvas.getContext('2d');
                    tempCanvas.width = c_w;
                    tempCanvas.height = c_h;
                    tempCtx.font = `${fontSize}px sans-serif`;
                    tempCtx.fillStyle = color;
                    tempCtx.textAlign = 'center';
                    tempCtx.textBaseline = 'middle';

                    const xInterval = (c_w - (text.length * fontSize) * rows) / (rows - 1);
                    const yInterval = (c_h - fontSize * cols) / (cols - 1);

                    // 添加三行三列水印
                    for (let i = 0; i < rows; i++) {
                        for (let j = 0; j < cols; j++) {
                            let x = i * xInterval + (i * (text.length * fontSize)) + (text.length * fontSize) / 2;
                            let y = j * yInterval + (j * fontSize) + fontSize / 2;
                            tempCtx.fillText(watermarkText, x, y);
                        }
                    }

                    ctx.save();
                    ctx.translate(c_w / 2, c_h / 2);
                    ctx.rotate(angle);
                    ctx.drawImage(tempCanvas, -c_w / 2, -c_h / 2);
                    ctx.restore();

                    //添加完水印的照片
                    // let warterPhotoBase64 = cvs.toDataURL('image/png');
                    // console.log('添加完水印的照片', warterPhotoBase64)
                };
            };
            reader.readAsDataURL(file);
        }
    }
};
</script>

相关推荐

  1. canvas实现照片添加水印

    2024-06-14 15:34:03       29 阅读
  2. 照片添加拍摄日期

    2024-06-14 15:34:03       36 阅读
  3. 【uniapp-小程序-video添加水印

    2024-06-14 15:34:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 15:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 15:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 15:34:03       82 阅读
  4. Python语言-面向对象

    2024-06-14 15:34:03       91 阅读

热门阅读

  1. 后端主流框架--Spring02

    2024-06-14 15:34:03       30 阅读
  2. Web前端开发必备:三大主流框架详细解析

    2024-06-14 15:34:03       32 阅读
  3. MongoDB 多层级查询

    2024-06-14 15:34:03       23 阅读
  4. 【DPDK学习路径】九、学习分支

    2024-06-14 15:34:03       26 阅读
  5. 第二节 单机版本redis部署

    2024-06-14 15:34:03       30 阅读
  6. 新视野大学英语2 词组 6.14

    2024-06-14 15:34:03       30 阅读
  7. 小程序项目业务逻辑回忆1

    2024-06-14 15:34:03       27 阅读
  8. u-boot的fpga命令

    2024-06-14 15:34:03       24 阅读
  9. Uniapp导航栏右侧自定义图标文字按钮

    2024-06-14 15:34:03       27 阅读