feat: add image watermark

This commit is contained in:
2026-03-07 22:14:29 +08:00
parent 25d3b7d847
commit b091ad73d6
5 changed files with 455 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
import { assertEquals } from "@std/assert";
import { addWatermark } from "./main.ts";
Deno.test("addWatermark - should add watermark to image", async () => {
const { default: sharp } = await import("sharp");
// 创建测试图片 (200x200 红色方块)
const testImagePath = "/tmp/test_image.png";
await sharp({
create: {
width: 200,
height: 200,
channels: 3,
background: { r: 255, g: 0, b: 0 },
},
}).png().toFile(testImagePath);
const outputPath = "/tmp/test_image_watermarked.png";
try {
// 添加水印(带背景色)
await addWatermark(testImagePath, {
text: "Test Watermark",
fontSize: 16,
color: "white",
backgroundColor: "#80000000",
backgroundPadding: 5,
marginRight: 10,
marginBottom: 10,
outputPath,
});
// 验证输出文件存在
const stat = await Deno.stat(outputPath);
assertEquals(stat.isFile, true);
} finally {
// 清理
try {
await Deno.remove(testImagePath);
} catch {
// Ignore
}
try {
await Deno.remove(outputPath);
} catch {
// Ignore
}
}
});