generated from eric861129/Cloud-Assets
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptimize.py
More file actions
263 lines (224 loc) · 9.93 KB
/
optimize.py
File metadata and controls
263 lines (224 loc) · 9.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import os
from PIL import Image
REPO_OWNER = "eric861129"
REPO_NAME = "Cloud-Assets"
# 嘗試從 GitHub Actions 環境變數獲取 owner/repo
github_repository = os.getenv("GITHUB_REPOSITORY")
if github_repository:
parts = github_repository.split("/")
if len(parts) == 2:
REPO_OWNER = parts[0]
REPO_NAME = parts[1]
BRANCH = "main"
CDN_BASE_URL = f"https://cdn.jsdelivr.net/gh/{REPO_OWNER}/{REPO_NAME}@{BRANCH}"
def optimize_images(directory=".", quality=80):
"""
遞迴掃描目錄,將圖片轉為 WebP 並刪除原圖。
"""
for root, dirs, files in os.walk(directory):
if ".git" in root:
continue
for filename in files:
if filename.lower().endswith((".png", ".jpg", ".jpeg")):
filepath = os.path.join(root, filename)
base = os.path.splitext(filename)[0]
output_path = os.path.join(root, f"{base}.webp")
try:
# 轉檔
with Image.open(filepath) as img:
img.save(output_path, "WEBP", quality=quality, method=6)
print(f"優化成功: {filepath} -> {output_path}")
# 刪除原圖
os.remove(filepath)
print(f"已刪除原圖: {filepath}")
except Exception as e:
print(f"處理 {filepath} 時出錯: {e}")
def generate_index_html(gallery_data):
"""
根據圖片資料生成一個美觀的 index.html 靜態頁面 (樹狀結構、預設折疊)。
"""
print("正在生成 index.html ...")
# 1. 建構樹狀結構
tree = {'files': [], 'subdirs': {}}
for folder_path, files in gallery_data.items():
if folder_path == "Root (根目錄)":
current = tree
current['files'] = files
else:
parts = folder_path.split("/")
current = tree
for part in parts:
if part not in current['subdirs']:
current['subdirs'][part] = {'files': [], 'subdirs': {}}
current = current['subdirs'][part]
current['files'] = files
# 2. 遞迴計算每個節點(含子目錄)的圖片總數
def count_total_images(node):
count = len(node['files'])
for sub in node['subdirs'].values():
count += count_total_images(sub)
return count
# 3. 遞迴生成 HTML
def render_tree(node, current_path=""):
html_parts = []
# 先顯示子目錄 (Folders),按名稱排序
for dirname in sorted(node['subdirs'].keys()):
sub_node = node['subdirs'][dirname]
new_path = f"{current_path}/{dirname}" if current_path else dirname
inner_content = render_tree(sub_node, new_path)
total_imgs = count_total_images(sub_node)
# 只有當該目錄或子目錄有圖片時才顯示
if total_imgs > 0:
folder_html = f"""
<details class="folder-section">
<summary>📁 {dirname} <span class="badge rounded-pill bg-secondary ms-1">{total_imgs}</span></summary>
<div class="nested-content">
{inner_content}
</div>
</details>
"""
html_parts.append(folder_html)
# 再顯示當前目錄的圖片 (Files)
if node['files']:
cards = []
for filename in node['files']:
if current_path:
img_path = f"{current_path}/{filename}"
else:
img_path = filename
full_url = f"{CDN_BASE_URL}/{img_path}"
md_code = f""
card_html = f"""
<div class="col-md-3 col-sm-6 col-12">
<div class="card h-100 shadow-sm">
<div class="img-container" onclick="window.open('{full_url}')">
<img src="{full_url}" class="card-img-top img-preview" alt="{filename}" loading="lazy">
</div>
<div class="card-body p-2">
<p class="card-text text-truncate small mb-2" title="{filename}">{filename}</p>
<div class="d-grid gap-1">
<button class="btn btn-xs btn-outline-primary copy-btn" onclick="copyToClipboard('{full_url}', this)">複製連結</button>
<button class="btn btn-xs btn-outline-secondary copy-btn" onclick="copyToClipboard('{md_code}', this)">複製 MD</button>
</div>
</div>
</div>
</div>
"""
cards.append(card_html)
images_grid = f"""
<div class="row g-2 mt-1 mb-3">
{''.join(cards)}
</div>
"""
html_parts.append(images_grid)
return "".join(html_parts)
content_html = render_tree(tree, "")
html_template = """
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cloud-Assets 圖庫</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; padding-top: 30px; padding-bottom: 50px; font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
/* 圖片卡片樣式 */
.card { border: none; transition: transform 0.2s; }
.card:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(0,0,0,0.1) !important; }
.img-container { height: 150px; overflow: hidden; background: #eee; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.img-preview { max-height: 100%; max-width: 100%; object-fit: contain; }
.btn-xs { padding: 0.1rem 0.4rem; font-size: 0.75rem; }
/* 樹狀結構與折疊樣式 */
.folder-section { margin-bottom: 8px; }
.nested-content {
margin-left: 18px;
padding-left: 12px;
border-left: 2px solid #e0e0e0; /* 樹狀連接線 */
margin-top: 5px;
}
details > summary {
list-style: none;
cursor: pointer;
padding: 8px 12px;
background: #fff;
border-radius: 6px;
border: 1px solid #dee2e6;
font-weight: 600;
display: inline-block;
min-width: 250px;
user-select: none;
position: relative;
}
details > summary:hover { background-color: #f1f3f5; border-color: #ced4da; }
/* 自定義箭頭 */
details > summary::marker { display: none; }
details > summary::before {
content: '▶';
display: inline-block;
margin-right: 8px;
font-size: 0.75rem;
color: #6c757d;
transition: transform 0.2s ease;
}
details[open] > summary::before { transform: rotate(90deg); }
details[open] > summary { margin-bottom: 5px; background-color: #e9ecef; border-color: #ced4da; }
h1 { font-size: 1.8rem; color: #343a40; }
</style>
</head>
<body>
<div class="container">
<div class="d-flex justify-content-between align-items-center mb-4 border-bottom pb-3">
<h1 class="m-0">📂 Cloud-Assets</h1>
<div>
<button class="btn btn-sm btn-outline-secondary me-2" onclick="toggleAll(true)">全部展開</button>
<button class="btn btn-sm btn-outline-secondary me-2" onclick="toggleAll(false)">全部折疊</button>
<a href="https://github.com/eric861129/Cloud-Assets" class="btn btn-sm btn-dark" target="_blank">GitHub</a>
</div>
</div>
<div id="gallery">
{content}
</div>
<footer class="mt-5 text-center text-muted small">
Generated by Cloud-Assets | <a href="#" onclick="window.scrollTo(0,0); return false;">Top</a>
</footer>
</div>
<script>
function copyToClipboard(text, btn) {
navigator.clipboard.writeText(text).then(() => {
const originalText = btn.innerText;
const originalClass = btn.className;
btn.innerText = "已複製";
btn.className = "btn btn-xs btn-success copy-btn";
setTimeout(() => {
btn.innerText = originalText;
btn.className = originalClass;
}, 1500);
});
}
function toggleAll(expand) {
document.querySelectorAll('details').forEach(d => {
if(expand) d.setAttribute('open', '');
else d.removeAttribute('open');
});
}
</script>
</body>
</html>
"""
final_html = html_template.replace("{content}", "".join(content_html))
with open("index.html", "w", encoding="utf-8") as f:
f.write(final_html)
print("index.html 生成完畢。")
if __name__ == "__main__":
optimize_images()
# 重新掃描以獲取最新的 WebP 資訊
current_gallery_data = {}
for root, dirs, files in os.walk("."):
if ".git" in root: continue
webp_files = [f for f in files if f.lower().endswith(".webp")]
if webp_files:
rel_dir = os.path.relpath(root, ".").replace("\\", "/")
if rel_dir == ".": rel_dir = "Root (根目錄)"
current_gallery_data[rel_dir] = sorted(webp_files)
generate_index_html(current_gallery_data)