Demo: 给图片添加自定义水印并下载

给图片添加自定义水印并下载

在这里插入图片描述

<template><div class="wrap"><div class="optea"><div class="file-upload"><p>选择图片</p><el-button type="text" style="color: #c00;"><label for="uploads"><i class="el-icon-upload2" style="margin-right: 5px;"></i>选择需要添加水印的图片</label></el-button><inputtype="file"id="uploads"hiddenaccept="image/png, image/jpeg, image/gif, image/jpg"@change="uploadImg($event, 1)"/></div><p>水印文字</p><el-inputv-model="watermarkOptions.text"placeholder="请输入水印内容"></el-input><p>字体颜色</p><el-color-picker v-model="watermarkOptions.color"></el-color-picker><p>旋转角度</p><el-sliderv-model="watermarkOptions.rotate":min="0":max="360"></el-slider><p>透明度</p><el-sliderv-model="watermarkOptions.alpha":min="0":max="100"></el-slider><p>文本间距</p><el-sliderv-model="watermarkOptions.width":min="0":max="100"></el-slider><p>字体大小</p><el-sliderv-model="watermarkOptions.fontSize":min="0":step="0.5":max="50"></el-slider></div><div><el-button@click="handleDown"class="download-btn"type="primary"plainicon="el-icon-download">下载水印图片</el-button><div class="preview" ref="previewImg"><img :src="preImg || defaultimg" alt="" /><div class="watermark" :style="{ background: paint }"></div></div></div></div>
</template>
<script>
/* eslint-disable */
import defaultimg from "@/assets/img/headPortrait.jpg";
import DomToImage from "dom-to-image";
export default {data() {return {defaultimg,preImg: "",watermarkOptions: {text: "仅供 xxx 验证使用",fontSize: 10,width: 5,color: "#000000",alpha: 35,rotate: 35}};},methods: {uploadImg(e, num) {//上传图片// this.option.imgvar file = e.target.files[0];if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {alert("图片类型必须是.gif,jpeg,jpg,png,bmp中的一种");return false;}var reader = new FileReader();reader.onload = e => {let data;if (typeof e.target.result === "object") {// 把Array Buffer转化为blob 如果是base64不需要data = window.URL.createObjectURL(new Blob([e.target.result]));} else {data = e.target.result;}if (num === 1) {this.preImg = data;}};// 转化为base64reader.readAsDataURL(file);// 转化为blob// reader.readAsArrayBuffer(file)},handleDown() {let node = this.$refs.previewImg;let that = this;DomToImage.toPng(node).then(function(dataUrl) {var oLink = document.createElement("a");oLink.download = "水印图片.png";oLink.href = dataUrl;oLink.click();that.$nextTick(() => {that.$message.success("水印图片下载成功");});}).catch(function(error) {console.error("oops, something went wrong!", error);that.$message.error("下载失败");});}},computed: {paint() {// 文字长度const wordWidth =this.watermarkOptions.fontSize *this.watermarkOptions.text.split("").length;const width = wordWidth + this.watermarkOptions.width;let svgText = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}px" height="${width}px"><text x="50%" y="50%"alignment-baseline="middle"text-anchor="middle"stroke="${this.watermarkOptions.color}"opacity="${this.watermarkOptions.alpha / 100}"transform="translate(${width / 2}, ${width / 2}) rotate(${this.watermarkOptions.rotate}) translate(-${width / 2}, -${width / 2})"font-weight="100"font-size="${this.watermarkOptions.fontSize}"font-family="microsoft yahe">${this.watermarkOptions.text}</text></svg>`;return `url(data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(svgText)))})`;}}
};
</script><style lang="scss" scoped>
.wrap {padding: 10px 20px;display: flex;justify-content: flex-start;align-items: center;p {margin: 5px 0;margin-bottom: 10px;font-weight: 600;}.download-btn {margin: 0 15px 15px;}.optea {width: 500px;}.preview {position: relative;margin-left: 20px;min-width: 750px;img {width: 100%;height: 100%;}.watermark {position: absolute;left: 0;top: 0;width: 100%;height: 100%;}}
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/614056.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序swiper实现层叠轮播图

在微信小程序中,需要实现展示5个&#xff0c;横向层叠的轮播图效果&#xff0c;轮播图由中间到2侧的依次缩小.如下图 使用原生小程序进行开发,没有使用Skyline模式&#xff0c;所以layout-type配置项也无效。所以基于swiper组件进行调整。 主要思路就是设置不同的样式&#xff…

sdbusplus:method同步调用通用函数

dbus的method操作的方式也比较类似,可以尝试封装成通用函数: //dbus_call.hpp #pragma once#include <utility> #include <boost/asio.hpp> #include <sdbusplus/asio/connection.hpp> #include <sdbusplus/bus.hpp> #include <sdbusplus/messag…

android启动流程

BootROM 这个固化在rom里 Bootloader 启动kernel前的准备工作&#xff0c;包括正常启动与recovery&#xff0c;烧写等不同做出判断 启动kernel会传些参数 重点是android启动流程 1. 驱动加载 比如usb, light, audio, camera, bt, wifi等 2. init 见system/core/init/init.c…

操作系统 进程相关

1 进程、线程、协程 定义 【Are u OKay&#xff1f;——协程、线程、进程】 https://www.bilibili.com/video/BV1Wr4y1A7DS/?share_sourcecopy_web&vd_source1e4d767755c593476743c8e4f64e18db 高并发&#xff1a;线程池&#xff0c;不要无休止的创建线程。--> task…

一起学习python类的属性装饰器@property

之前文章我们介绍了class的一些通用功能&#xff0c;比如类属性/类方法/实例属性/实例方法等&#xff0c;之前的属性可以直接修改和访问&#xff08;设置私有属性&#xff0c;不能直接访问,可通过对象名._[类名][属性名]的方式访问&#xff09;&#xff0c;没有一些权限的控制逻…

java--科星互联ID刷卡器TTS语音版,UDP协议实现语音播报

import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UdpVoiceSender { private static final String IP_ADDRESS "192.168.1.100"; // 刷卡器IP地址 private stati…

计算机速成课Crash Course - 18. 操作系统

今天继续计算机速成课Crash Course的系列讲解。 更多技术文章&#xff0c;全网首发公众号 “摸鱼IT” 锁定 -上午11点 - &#xff0c;感谢大家关注、转发、点赞&#xff01; 计算机速成课Crash Course - 17. 集成电路&摩尔定律 18. 操作系统 1940,1950 年代的电脑&#…

常用机床类型的用途和介绍

随着市场对机加工需求的提升&#xff0c;机械加工的技术精度也随之提高&#xff0c;机床的种类也就越来越多。 根据加工方法和使用的工具进行分类&#xff0c;国家将机床编制为11类&#xff1a;车床、钻床、镗床、磨床、齿轮加工机床、螺纹加工机床、铣床、刨床、拔床、锯床等…

pyspark 使用udf 进行预测,发现只起了一个计算节点

PySpark UDF 只使用一个计算节点的问题 原因分析 默认的并行度设置 PySpark在执行UDF&#xff08;用户定义函数&#xff09;时&#xff0c;默认可能不会利用所有可用的计算节点。这是因为UDF通常在单个节点上执行&#xff0c;并且如果没有正确设置分区&#xff0c;可能会导致数…

Windows下Python+PyCharm+miniconda+Cuda/GPU 安装步骤

1. 官网安装Python 3.9 Python Release Python 3.9.0 | Python.org 2. 安装pycharm https://download.jetbrains.com/python/pycharm-professional-2023.3.2.exe 3. 安装miniconda Miniconda — miniconda documentation 4. 安装完miniconda 创建虚拟环境 conda create …

Transformer学习(一)

文章目录 transformer介绍为什么处理长序列时会出现梯度消失和梯度爆炸的问题transformer为什么可以用在图像处理上&#xff1f; transformer介绍 Transformer 是一种在深度学习中广泛使用的模型结构&#xff0c;最初由 Vaswani 等人在 “Attention is All You Need” 一文中提…

Java 关于 Object 类中的 finalize() 和 hashCode() 方法

关于 Object 类中的 finalize() 方法。 1、在 Object 类中的源代码&#xff1a; protected void finalize() throws Throwable{} GC&#xff1a;负责调用 finalize() 方法。 2、finalize() 方法只有一个方法体&#xff0c;里面没有代码&#xff0c;而且这个方法是 protected 修…

2023-12-29 贪心算法 分发饼干和摆动序列以及最大子数组和

贪心算法 什么是贪心算法&#xff1f; 就是每一阶段的最优解&#xff0c;从局部的最优解达到全局的最优解&#xff01; 最好用的策略就是举反例&#xff0c;如果想不到反例&#xff0c;那么就试一试贪心吧。 贪心算法一般分为如下四步&#xff1a; 将问题分解为若干个子问…

【Python】win10 版Anaconda下载安装与认识 (2024版)

下载 Anaconda下载地址 还是蛮大的1GB: 安装 安装的话一般都是傻瓜式安装&#xff0c;选定好自己的安装目录就是下一步下一步就OK了&#xff0c;这里保存了一些安装过程中的记录&#xff1a; 安装成功&#xff1a; 安装过程时间还是有点长的&#xff0c;不知道是我电脑弱…

归并排序例题——逆序对的数量

做道简单一点的题巩固一下 归并排序实现步骤 将整个区间 [l, r] 划分为 [l, mid] 和 [mid1, r]。 递归排序 [l, mid] 和 [mid1, r]。 将左右两个有序序列合并为一个有序序列。 题目描述 给定一个长度为 n 的整数数列&#xff0c;请计算数列中的逆序对的数量。 逆序对的定义…

golang 生成一年的周数

// GetWeekTimeCycleForGBT74082005 获取星期周期 中华人民共和国国家标准 GB/T 7408-2005 // 参数 year 年份 GB/T 7408-2005 func GetWeekTimeCycleForGBT74082005(year int) (*[]TimeCycle, error) {var yearstart time.Time //当年最开始一天var yearend time.Time //当年…

系列三、Spring Security中自定义用户名/密码

一、Spring Security中自定义用户名/密码 1.1、自定义用户名/密码 1.1.1、配置文件中配置 spring.security.user.nameroot spring.security.user.password123456 1.1.2、定义基于内存的用户 /*** Author : 一叶浮萍归大海* Date: 2024/1/11 21:50* Description:*/ Configu…

Java 日期接收报错:could not be parsed, unparsed text found at index 10(已解决)

文章目录 问题背景代码解决方法问题背景 使用 Element-UI 的 el-date-picker 组件 将日期转给Java,Java报错 java.time.format.DateTimeParseException: Text ‘2024-01-03T16:00:00.000Z’ could not be parsed, unparsed text found at index 10 代码 Vue<el-date-picke…

关于SpringMVC前后端传值总结

一、传递方式 1、查询参数&路径参数 查询参数&#xff1a; URI:/teachers?typeweb GetMapping("/klasses/teachers") public List<Teacher> getKlassRelatedTeachers(String type ) { ... }如果查询参数type与方法的名称相同&#xff0c;则直接将web传入…

EM planner 论文阅读

论文题目&#xff1a;Baidu Apollo EM Motion Planner 0 前言 EM和Lattice算法对比 EM plannerLattice Planner参数较多&#xff08;DP/QP&#xff0c;Path/Speed&#xff09;参数少且统一化流程复杂流程简单单周期解空间受限简单场景解空间较大能适应复杂场景适合简单场景 …