Skip to content

Commit 2760674

Browse files
Update compress-images.yml
1 parent 3e055ab commit 2760674

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

.github/workflows/compress-images.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,43 @@ jobs:
2828
run: |
2929
set -e
3030
31-
# 需要处理的文件(排除已经是 webp 的)
3231
mapfile -t FILES < <(git ls-files | grep -Ei '\.(jpg|jpeg|png)$')
3332
34-
# 如果没有新图,直接退出
3533
if [ ${#FILES[@]} -eq 0 ]; then
3634
echo "No images to optimize."
3735
exit 0
3836
fi
3937
40-
# 目标上限(字节)
4138
TARGET=200000
42-
43-
# 最大宽度(超过就等比缩小;自行调整,如 1600/1920)
4439
MAXW=1920
4540
4641
for f in "${FILES[@]}"; do
4742
echo "Processing: $f"
4843
49-
# 临时输出名(先转 webp)
5044
out="${f%.*}.webp"
5145
52-
# 先做一次“温和”压缩与缩放
46+
# 初次压缩
5347
npx @squoosh/cli --webp auto --resize "max_width=${MAXW}" -d /tmp "$f" >/dev/null 2>&1 || true
5448
cand="/tmp/${out##*/}"
5549
56-
# 如果仍超 200KB,则逐步降低质量重试
5750
if [ -f "$cand" ]; then
58-
size=$(stat -c%s "$cand" 2>/dev/null || stat -f%z "$cand")
51+
size=$(wc -c < "$cand")
5952
else
6053
size=999999999
6154
fi
6255
56+
# 如果超 200KB,逐步降低质量
6357
if [ "$size" -gt "$TARGET" ]; then
6458
for q in 85 80 75 70 65 60; do
6559
npx @squoosh/cli --webp "quality=${q}" --resize "max_width=${MAXW}" -d /tmp "$f" >/dev/null 2>&1 || true
6660
cand="/tmp/${out##*/}"
67-
size=$(stat -c%s "$cand" 2>/dev/null || stat -f%z "$cand")
61+
size=$(wc -c < "$cand")
6862
echo " Try quality=$q -> ${size} bytes"
6963
[ "$size" -le "$TARGET" ] && break
7064
done
7165
fi
7266
73-
# 用 webp 替换原图(也可以选择另存到 assets/img/optimized/)
67+
# 替换原图
7468
if [ -f "$cand" ]; then
7569
mv "$cand" "$out"
7670
git rm -f --quiet "$f"
@@ -82,5 +76,5 @@ jobs:
8276
- name: Commit optimized images
8377
uses: EndBug/add-and-commit@v9
8478
with:
85-
message: "chore: optimize images to ~200KB (webp, resize, quality step-down)"
79+
message: "chore: optimize images to ~200KB (webp)"
8680
add: "."

0 commit comments

Comments
 (0)