Add some status and safety checks
This commit is contained in:
+11
-3
@@ -8,7 +8,15 @@
|
||||
echo "Resizing large JPGs in $(pwd)"
|
||||
|
||||
find . -regextype posix-egrep -regex ".*\.(jpg|JPG|JPEG|jpeg)$" -print0 | while IFS= read -r -d '' f
|
||||
#echo "Resizing $(f)"
|
||||
#do mogrify -resize 2800000@@\> -sampling-factor 4:2:0 -strip -quality 85% -colorspace RGB $f
|
||||
do ~/Applications/magick "$f" -resize 8000000@@\> -sampling-factor 4:2:0 -strip -quality 85% "$f"
|
||||
do
|
||||
# Get file size before resizing
|
||||
SIZE_BEFORE=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null)
|
||||
|
||||
~/Applications/magick "$f" -resize 8000000@@\> -sampling-factor 4:2:0 -strip -quality 85% "$f"
|
||||
|
||||
# Get file size after resizing
|
||||
SIZE_AFTER=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null)
|
||||
|
||||
# Display before/after sizes
|
||||
echo "Resized $f: $(numfmt --to=iec-i --suffix=B $SIZE_BEFORE) -> $(numfmt --to=iec-i --suffix=B $SIZE_AFTER)"
|
||||
done
|
||||
|
||||
@@ -17,7 +17,13 @@ cleanup_temp() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Count total files
|
||||
TOTAL_FILES=$#
|
||||
CURRENT_FILE=0
|
||||
|
||||
for INFILE in "$@"; do
|
||||
CURRENT_FILE=$((CURRENT_FILE + 1))
|
||||
echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE"
|
||||
# Absolute path to old file
|
||||
#OLDFILE=$(realpath "${INFILE}")
|
||||
OUTFILE="${INFILE%.zip}-compress.zip"
|
||||
@@ -70,6 +76,14 @@ for INFILE in "$@"; do
|
||||
|
||||
# Preserve file modification time
|
||||
touch -r "$INFILE" "$OUTFILE"
|
||||
|
||||
# Check that OUTFILE exists and is not empty before deleting INFILE
|
||||
if [ ! -s "$OUTFILE" ]; then
|
||||
echo "Error: Output file is empty or does not exist. Keeping original file."
|
||||
cleanup_temp
|
||||
continue
|
||||
fi
|
||||
|
||||
rm "$INFILE"
|
||||
mv "$OUTFILE" "$INFILE"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user