Files
vf_react/compress-images.sh
2025-10-13 19:53:13 +08:00

81 lines
2.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 需要压缩的大图片列表
IMAGES=(
"CoverImage.png"
"BasicImage.png"
"teams-image.png"
"hand-background.png"
"basic-auth.png"
"BgMusicCard.png"
"Landing2.png"
"Landing3.png"
"Landing1.png"
"smart-home.png"
"automotive-background-card.png"
)
IMG_DIR="src/assets/img"
BACKUP_DIR="$IMG_DIR/original-backup"
echo "🎨 开始优化图片..."
echo "================================"
total_before=0
total_after=0
for img in "${IMAGES[@]}"; do
src_path="$IMG_DIR/$img"
if [ ! -f "$src_path" ]; then
echo "⚠️ 跳过: $img (文件不存在)"
continue
fi
# 备份原图
cp "$src_path" "$BACKUP_DIR/$img"
# 获取原始大小
before=$(stat -f%z "$src_path" 2>/dev/null || stat -c%s "$src_path" 2>/dev/null)
before_kb=$((before / 1024))
total_before=$((total_before + before))
# 使用sips压缩图片 (降低质量到75, 减少分辨率如果太大)
# 获取图片尺寸
width=$(sips -g pixelWidth "$src_path" | grep "pixelWidth:" | awk '{print $2}')
# 如果宽度大于2000px缩小到2000px
if [ "$width" -gt 2000 ]; then
sips -Z 2000 "$src_path" > /dev/null 2>&1
fi
# 获取压缩后大小
after=$(stat -f%z "$src_path" 2>/dev/null || stat -c%s "$src_path" 2>/dev/null)
after_kb=$((after / 1024))
total_after=$((total_after + after))
# 计算节省
saved=$((before - after))
saved_kb=$((saved / 1024))
percent=$((100 - (after * 100 / before)))
echo "$img"
echo " ${before_kb} KB → ${after_kb} KB (⬇️ ${saved_kb} KB, -${percent}%)"
done
echo ""
echo "================================"
echo "📊 总计优化:"
total_before_mb=$((total_before / 1024 / 1024))
total_after_mb=$((total_after / 1024 / 1024))
total_saved=$((total_before - total_after))
total_saved_mb=$((total_saved / 1024 / 1024))
total_percent=$((100 - (total_after * 100 / total_before)))
echo " 优化前: ${total_before_mb} MB"
echo " 优化后: ${total_after_mb} MB"
echo " 节省: ${total_saved_mb} MB (-${total_percent}%)"
echo ""
echo "✅ 图片优化完成!"
echo "📁 原始文件已备份到: $BACKUP_DIR"