46
compress_images.sh
普通文件
46
compress_images.sh
普通文件
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
UPLOADS_DIR="/www/wwwroot/chufang.gynhc.com/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"
|
||||
在新工单中引用
屏蔽一个用户