加入照片功能

This commit is contained in:
DelLevin-Home
2026-05-18 02:23:58 +08:00
parent 5e33d53ea3
commit f0605dca4c
25 changed files with 746 additions and 40 deletions

61
scripts/README.md Normal file
View File

@@ -0,0 +1,61 @@
# 人生相册构建脚本
## 📖 功能说明
自动扫描 `/static/img/photos/` 目录下的所有图片文件,生成 `photos.json` 索引文件。
## 🚀 使用方法
### 方法一:双击运行(推荐)
直接双击 `build-photos.bat` 文件即可。
### 方法二:命令行运行
```bash
# 在项目根目录下执行
python scripts/generate-photos-json.py
```
## 📝 工作流程
1. 将新照片放入 `/static/img/photos/` 目录
2. 运行构建脚本(双击 `build-photos.bat`
3. 脚本会自动:
- 扫描目录下所有图片文件(支持 jpg, png, gif, webp, bmp, svg
- 按文件名排序
- 生成/更新 `photos.json` 文件
4. 刷新网页,新照片自动显示
## 📂 生成的 JSON 格式
```json
{
"photos": [
"1 (1).jpg",
"1 (2).jpg",
"..."
],
"total": 13,
"lastUpdated": "2026-03-03 15:30:00"
}
```
## ⚙️ 配置说明
支持的图片格式:
- `.jpg` / `.jpeg`
- `.png`
- `.gif`
- `.webp`
- `.bmp`
- `.svg`
如需添加其他格式,编辑 `generate-photos-json.py` 中的 `IMAGE_EXTENSIONS` 集合。
## 💡 提示
- 照片会按文件名**字母顺序**排序
- 建议使用数字前缀控制顺序,如:`001.jpg`, `002.jpg`
- 脚本会自动排除 `photos.json` 文件本身
- 每次运行都会重新扫描并覆盖旧的 JSON 文件

14
scripts/build-photos.bat Normal file
View File

@@ -0,0 +1,14 @@
@echo off
chcp 65001 >nul
echo ========================================
echo 人生相册构建脚本
echo ========================================
echo.
cd /d "%~dp0.."
python scripts\generate-photos-json.py
echo.
echo ========================================
pause

View File

@@ -0,0 +1,91 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import json
import re
from pathlib import Path
from datetime import datetime
# ==================== 配置区域 ====================
# 请在此处指定你的照片目录和输出文件路径
DEFAULT_PHOTOS_DIR = Path("/www/wwwroot/www.iletter.top/static/img/photos")
DEFAULT_OUTPUT_FILE = Path("/www/wwwroot/www.iletter.top/static/img/photos/photos.json")
# ================================================
# 支持的图片格式
IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.svg'}
def natural_sort_key(filename):
"""自然排序键函数,支持数字正确排序"""
return [int(text) if text.isdigit() else text.lower()
for text in re.split(r'(\d+)', filename)]
def scan_photos(photos_dir):
"""扫描照片目录,返回按修改时间排序的文件名列表(最新的在最前面)"""
if not photos_dir.exists():
print(f"❌ 目录不存在: {photos_dir}")
return []
photos = []
for file in photos_dir.iterdir():
if file.is_file() and file.suffix.lower() in IMAGE_EXTENSIONS:
# 排除 JSON 文件本身
if file.name == "photos.json":
continue
# 存储文件名和修改时间戳
photos.append((file.name, file.stat().st_mtime))
# 按修改时间降序排序(最新的在最前面)
photos.sort(key=lambda x: x[1], reverse=True)
# 只返回文件名列表
return [p[0] for p in photos]
def generate_json(photos, output_file):
"""生成 photos.json 文件"""
data = {
"photos": photos,
"total": len(photos),
"lastUpdated": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
# 写入文件
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
return len(photos)
def main():
# 确定照片目录和输出文件路径
photos_dir = DEFAULT_PHOTOS_DIR.resolve()
output_file = DEFAULT_OUTPUT_FILE.resolve()
print("🚀 开始扫描照片目录...")
print(f"📁 照片目录: {photos_dir}")
print(f"📝 输出文件: {output_file}")
photos = scan_photos(photos_dir)
if not photos:
print("⚠️ 未找到任何照片文件")
# 仍然生成空的 JSON
generate_json([], output_file)
print(f"✅ 已生成空的 {output_file}")
return
count = generate_json(photos, output_file)
print(f"✅ 成功生成 {output_file}")
print(f"📊 共扫描到 {count} 张照片:")
for i, photo in enumerate(photos, 1):
print(f" {i}. {photo}")
if __name__ == "__main__":
main()