博客版权声明与图片水印方案


想起来要给博文加点保护,一开始想直接粗暴地 ban 掉任何复制和下载,被 Gemini 锐评不够淑女且没啥用。最后选择给复制加点版权声明和给图片加水印。

增加版权说明

打开themes/matery/layout/_partial/footer.ejs在靠最后的地方加上下面这段,done。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script>
(function () {
var MIN_COPY_LEN = 20;
var BLOG_NAME = "<%- (config.title || config.author || '本站') %>";
var COPYRIGHT_TEXT = "著作权归作者所有。";

document.addEventListener("copy", function (e) {
var sel = window.getSelection ? window.getSelection() : null;
var text = sel ? sel.toString() : "";
if (!text || text.length <= MIN_COPY_LEN) return;

var copytext =
text +
"\n\n--------------------------------\n" +
COPYRIGHT_TEXT +
"\n来源:" +
BLOG_NAME +
"\n链接:" +
window.location.href;

if (e.clipboardData) {
e.preventDefault();
e.clipboardData.setData("text/plain", copytext);
} else if (window.clipboardData) {
e.preventDefault();
window.clipboardData.setData("text", copytext);
}
});
})();
</script>

增加图片水印

试了两种方法:一种是动态加水印,一种是直接处理好静态图片(用脚本批处理)。

hexo本身自带了增加水印的插件npm install hexo-images-watermark --save可以安装,但是捯饬半天之后我还是选择了老老实实处理静态图片。理由如下:

  • 动态加水印要指定范围会很麻烦(你不可能把网站所有的图片都加水印吧)
  • 编译慢……图片越多启动越慢……
  • 静态处理图片的话更方便自定义需求,比如水印深浅

so我最后写了一个脚本处理图片(不过如果选择静态处理的话,也可以用普通的修图软件达到目的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
from PIL import Image

def add_fullscreen_watermark(input_dir, watermark_path, output_dir, opacity=0.15):
# 1. Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)

# 2. Open the watermark image and convert to RGBA mode
try:
original_watermark = Image.open(watermark_path).convert("RGBA")
except Exception as e:
print(f"Error: Could not open watermark image: {e}")
return

# 3. Adjust watermark transparency
r, g, b, a = original_watermark.split()
a = a.point(lambda p: p * opacity)
original_watermark.putalpha(a)

valid_extensions = ('.png', '.jpg', '.jpeg', '.bmp')

# 4. Iterate through all files in the input directory
for filename in os.listdir(input_dir):
if filename.lower().endswith(valid_extensions):
img_path = os.path.join(input_dir, filename)
try:
# Open the base image
base_image = Image.open(img_path).convert("RGBA")
img_w, img_h = base_image.size

# Create the final composite canvas
combined = Image.new('RGBA', base_image.size, (0, 0, 0, 0))
combined.paste(base_image, (0, 0))

# 5. Handle Scaling and Tiling
# Get watermark original dimensions
wm_orig_w, wm_orig_h = original_watermark.size

# Scale watermark width to match image width, maintaining aspect ratio
target_wm_w = img_w
target_wm_h = int(wm_orig_h * (target_wm_w / wm_orig_w))

try:
resample_filter = Image.Resampling.LANCZOS
except AttributeError:
resample_filter = Image.LANCZOS

watermark_scaled = original_watermark.resize((target_wm_w, target_wm_h), resample_filter)

# 6. Overlay the watermark multiple times (Vertical Tiling)
# This ensures long images are covered without stretching the watermark
y_offset = 0
while y_offset < img_h:
combined.paste(watermark_scaled, (0, y_offset), mask=watermark_scaled)
y_offset += target_wm_h

# 7. Save the processed image
output_path = os.path.join(output_dir, filename)

if filename.lower().endswith(('.jpg', '.jpeg')):
combined.convert("RGB").save(output_path, quality=95)
else:
combined.save(output_path)

print(f"Successfully processed: {filename}")
except Exception as e:
print(f"Error processing {filename}: {e}")

if __name__ == "__main__":
# Configuration paths
INPUT_DIR = r"这个是需要加水印的图片所在的绝对路径"
WATERMARK_PATH = r"这个是水印图片的绝对路径"
OUTPUT_DIR = os.path.join(INPUT_DIR, "watermarked")

# Opacity 0.15 is subtle; adjust if needed
add_fullscreen_watermark(INPUT_DIR, WATERMARK_PATH, OUTPUT_DIR, opacity=0.15)

print("\nAll tasks completed! Long images have been tiled instead of stretched.")

👉 点击这里继续阅读:《Hexo Matery 代码显示问题排查与修复》


Author: linda1729
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source linda1729 !
评论
  TOC