一、前端代码
- watermark.js 工具类
function addWatermarkAndUpload(file, watermarks) {
return new Promise(((resolve, reject) => {
const reader = new FileReader();
reader.onload = function (event) {
const img = new Image();
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
let fontSize = 100
ctx.font = fontSize + 'px Arial';
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
let x = 20
let y = 20
for (let i = 0; i < watermarks.length; i++) {
y = watermarkHandle(ctx, watermarks[i], canvas.width, canvas.height, fontSize, x, y)
}
canvas.toBlob((blob) => {
if (blob) {
blob.name = file.name
resolve(blob);
} else {
reject(new Error('Failed to create blob from canvas'));
}
}, 'image/png');
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
}));
}
function watermarkHandle(ctx, watermark, width, height, fontSize, x, y) {
let lineHeight = fontSize + 10
let line = '';
const list = [];
for (let i = 0; i < watermark.length; i++) {
const testLine = line + watermark[i];
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if (testWidth < width - fontSize && i < watermark.length - 1) {
line = testLine;
} else {
list.push(line)
line = watermark[i];
}
}
const testLine = line + list[list.length - 1];
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if (testWidth < width - fontSize) {
list[list.length - 1] = list[list.length - 1] + line
} else {
list.push(line)
}
for (let i = list.length - 1; i >= 0; i--) {
ctx.fillText(list[i], x, height - y);
y += lineHeight;
}
return y;
}
- 使用
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片水印并上传示例</title>
</head>
<body>
<input type="file" id="imageFile" accept="image/*">
<button onclick="addWatermark()">添加水印并上传</button>
<script th:src="@{/js/watermark.js}"></script>
<script> function addWatermark() {
const imageFileInput = document.getElementById('imageFile'); const file = imageFileInput.files[0]; if (!file) {
alert('请先选择图片!'); return; } var watermarks = [] watermarks.push("广西壮族自2心2圩街2汇11111111111111111111111112") watermarks.push("2024-05-11 19:13:04 ") watermarks.push("李华 130733376") const promises = []; for (let i = 0; i < 3; i++) {
promises.push(addWatermarkAndUpload(file, watermarks)) } Promise.all(promises) .then(results => {
console.log(results); const formData = new FormData(); for (let i = 0; i < results.length; i++) {
console.log(results[i]) formData.append('files', results[i], results[i].name); } return formData; }) .then(formData => {
return fetch('/common/uploads', {
method: 'POST', body: formData }); }) .then(response => {
return response.json(); }) .then(data => {
console.log(data); }) .catch(error => {
console.error('Error:', error); }) } </script>
</body>
</html>
二、完整前后端代码
全部代码
文章评论