当前位置:网站首页>canvas 实现图片新增水印
canvas 实现图片新增水印
2022-06-24 19:40:00 【前端同学】
给网页图片添加水印是个常见的功能,也是互联网内容作者保护自己版权的方法之一。本文简单记录一下借助canvas在前端实现图片添加水印的实现方法。
canvas元素其实就是一个画布,我们可以很方便地绘制一些文字、线条、图形等,它也可以将一个img标签里渲染的图片画在画布上。
const img: any = new Image();
img.src =
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp2-q.mafengwo.net%2Fs11%2FM00%2FB6%2F7B%2FwKgBEFt_tvGAFvHxAAFIRKQnOBw21.jpeg&refer=http%3A%2F%2Fp2-q.mafengwo.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658649916&t=6270eb9f963c25eb9af5cc101fe42e38';
img.crossOrigin = 'anonymous';
img.onload = function () {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx: any = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
ctx.font = '100px 宋体';
ctx.fillStyle = 'rgba(255, 255, 255)';
ctx.fillText('前端同学出品', img.width - 500, img.height - 50, 500); //文本 距离左上角位置,允许最大像素
const base64Url = canvas.toDataURL();
let result = new Image();
result.width = 300;
result.height = 300;
result.src = base64Url;
document.body.appendChild(result);
console.log(base64Url);
console.log(base64toBlob(base64Url));
console.log(translateBase64ImgToBlob(base64Url, 'image/jpeg'));
};
/*
Base64 转成 Blob
*/
function base64toBlob(dataurl: any) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
/*
Bolb 转 URL
原理: 利用URL.createObjectURL为blob对象创建临时的URL
*/
function translateBase64ImgToBlob(base64: any, contentType: any) {
var arr = base64.split(','); //去掉base64格式图片的头部
var bstr = atob(arr[1]); //atob()方法将数据解码
var leng = bstr.length;
var u8arr = new Uint8Array(leng);
while (leng--) {
u8arr[leng] = bstr.charCodeAt(leng); //返回指定位置的字符的 Unicode 编码
}
var blob = new Blob([u8arr], { type: contentType });
var blobImg: any = {};
blobImg.url = URL.createObjectURL(blob); //创建URL
blobImg.name = new Date().getTime() + '.png';
return blobImg;
}

这样就完整地给图片添加了水印效果,
边栏推荐
- Virtual private network foundation
- The difference between interceptor and filter
- OSPF basic content
- Learn more about the practical application of sentinel
- 京东618会议平板排行榜公布,新锐黑马品牌会参谋角逐前三名,向国货老大华为学习
- 1. fully explain the basic principles of IPSec
- 中国SSD行业企业势力全景图
- O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
- Learn more about redis' eight data types and application scenario analysis
- MySQL + JSON = King fried!!
猜你喜欢
Based on the codeless platform, users deeply participated in the construction, and digital data + Nanjing Fiberglass Institute jointly built a national smart laboratory solution

Environment configuration | vs2017 configuring openmesh source code and environment

ACL (access control list) basic chapter - Super interesting learning network
CA Zhouji - the first lesson in 2022 rust

Learn more about the practical application of sentinel

Combine pod identity in aks and secret in CSI driver mount key vault

电力系统| IEEE论文投稿流程

MySQL + JSON = King fried!!

Analyze the implementation process of oauth2 distributed authentication and authorization based on the source code

Leetcode: calculate the number of elements less than the current element on the right (sortedlist+bisect\u left)
随机推荐
China smallpox vaccine market trend report, technical innovation and market forecast
MySQL夺命10问,你能坚持到第几问?
[untitled]
High level application of SQL statements in MySQL database (I)
win10或win11打印机无法打印
开发规范~参数校验异常、异常返回提示切面
Concurrency of heap memory allocation
环境配置 | VS2017配置OpenMesh源码和环境
Stop using it indiscriminately. This is the real difference between @validated and @valid!!!
双亲委派机制
【个人实验报告】
Chapter 10 project stakeholder management
cat写多行内容到文件
Web security XSS foundation 06
OSPF basic content
详细了解Redis的八种数据类型及应用场景分析
Row and column differences in matrix construction of DX HLSL and GL glsl
Envoy obtain the real IP address of the client
Memory alignment of structures
In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services