47 行
1.6 KiB
Bash
47 行
1.6 KiB
Bash
#!/bin/bash
|
|
|
|
UPLOADS_DIR="/www/wwwroot/chufang.gynhc.com/public/uploads"
|
|
LOG_FILE="/tmp/image_compress.log"
|
|
TOTAL_SIZE_BEFORE=0
|
|
TOTAL_SIZE_AFTER=0
|
|
COUNT=0
|
|
|
|
echo "开始压缩图片..." | tee -a "$LOG_FILE"
|
|
echo "时间: $(date)" | tee -a "$LOG_FILE"
|
|
|
|
echo "" | tee -a "$LOG_FILE"
|
|
echo "=== 压缩 JPG/JPEG 图片 ===" | tee -a "$LOG_FILE"
|
|
|
|
find "$UPLOADS_DIR" -type f \( -name "*.jpg" -o -name "*.jpeg" \) | while read file; do
|
|
SIZE_BEFORE=$(stat -c%s "$file")
|
|
jpegoptim --max=85 --strip-all --preserve --totals "$file" >> "$LOG_FILE" 2>&1
|
|
SIZE_AFTER=$(stat -c%s "$file")
|
|
SAVED=$((SIZE_BEFORE - SIZE_AFTER))
|
|
PERCENT=$((SAVED * 100 / SIZE_BEFORE))
|
|
|
|
if [ $SAVED -gt 0 ]; then
|
|
echo "压缩: $file" | tee -a "$LOG_FILE"
|
|
echo " 原始: $SIZE_BEFORE 字节, 压缩后: $SIZE_AFTER 字节, 节省: $SAVED 字节 ($PERCENT%)" | tee -a "$LOG_FILE"
|
|
fi
|
|
done
|
|
|
|
echo "" | tee -a "$LOG_FILE"
|
|
echo "=== 压缩 PNG 图片 ===" | tee -a "$LOG_FILE"
|
|
|
|
find "$UPLOADS_DIR" -type f -name "*.png" | while read file; do
|
|
SIZE_BEFORE=$(stat -c%s "$file")
|
|
optipng -o2 -preserve "$file" >> "$LOG_FILE" 2>&1
|
|
SIZE_AFTER=$(stat -c%s "$file")
|
|
SAVED=$((SIZE_BEFORE - SIZE_AFTER))
|
|
PERCENT=$((SAVED * 100 / SIZE_BEFORE))
|
|
|
|
if [ $SAVED -gt 0 ]; then
|
|
echo "压缩: $file" | tee -a "$LOG_FILE"
|
|
echo " 原始: $SIZE_BEFORE 字节, 压缩后: $SIZE_AFTER 字节, 节省: $SAVED 字节 ($PERCENT%)" | tee -a "$LOG_FILE"
|
|
fi
|
|
done
|
|
|
|
echo "" | tee -a "$LOG_FILE"
|
|
echo "压缩完成!" | tee -a "$LOG_FILE"
|
|
echo "详细日志请查看: $LOG_FILE" | tee -a "$LOG_FILE"
|