55#
66# Tunables: Defaults
77#
8- export dry_run=true # if true, do not perform any file modifications
8+ export dry_run=false # if true, do not perform any file modifications
99export quality=85 # quality for webp conversion (1-100)
1010export max_width=1600 # maximum width before resizing
1111export target_width=1280 # target width for resizing
@@ -78,8 +78,12 @@ process_images() {
7878 echo " Checking $img ..."
7979 width=$( identify -format " %w" " $img " 2> /dev/null || echo 0)
8080
81- # Get file size in KB (macOS stat is different)
82- filesize=$( stat -c%s " $img " 2> /dev/null || echo 0)
81+ # Get file size in KB (handle both macOS and Linux)
82+ if [[ " $OSTYPE " == " darwin" * ]]; then
83+ filesize=$( stat -f%z " $img " 2> /dev/null || echo 0)
84+ else
85+ filesize=$( stat -c%s " $img " 2> /dev/null || echo 0)
86+ fi
8387 kbsize=$(( filesize / 1024 ))
8488
8589 # Define new filename
@@ -107,8 +111,17 @@ process_images() {
107111 fi
108112 echo " Optimizing $img (${kbsize} KB, ${width} px), quality=$quality "
109113 # Resize and convert to WebP. If error occurs, capture it and exit.
110- error=$( magick " $img " -resize ${target_width} x\> -quality $quality -define webp:lossless=true " $webp_img " 2>&1 )
111- convert_status=$?
114+ # Try 'magick' first (newer ImageMagick), fall back to 'convert' (older/traditional)
115+ if command -v magick & > /dev/null; then
116+ error=$( magick " $img " -resize ${target_width} x\> -quality $quality -define webp:lossless=true " $webp_img " 2>&1 )
117+ convert_status=$?
118+ elif command -v convert & > /dev/null; then
119+ error=$( convert " $img " -resize ${target_width} x\> -quality $quality -define webp:lossless=true " $webp_img " 2>&1 )
120+ convert_status=$?
121+ else
122+ echo " ⚠️ Neither 'magick' nor 'convert' command found. Please install ImageMagick."
123+ exit 1
124+ fi
112125 if [ $convert_status -ne 0 ]; then
113126 echo " ⚠️ Error converting $img to WebP format."
114127 if [ -z " ${error} " ]; then
@@ -130,8 +143,14 @@ process_images() {
130143 find " $img_dir " " $( dirname " $img_dir " ) " -name " *.md" 2> /dev/null | while read -r md_file; do
131144 if grep -q " $img_name " " $md_file " ; then
132145 echo " Replacing $img_name → $webp_name in $md_file "
133- sed -i ' ' " s|($img_name |(${webp_name} |g" " $md_file "
134- sed -i ' ' " s|/$img_name |/${webp_name} |g" " $md_file "
146+ # Handle sed differences between macOS and Linux
147+ if [[ " $OSTYPE " == " darwin" * ]]; then
148+ sed -i ' ' " s|($img_name |(${webp_name} |g" " $md_file "
149+ sed -i ' ' " s|/$img_name |/${webp_name} |g" " $md_file "
150+ else
151+ sed -i " s|($img_name |(${webp_name} |g" " $md_file "
152+ sed -i " s|/$img_name |/${webp_name} |g" " $md_file "
153+ fi
135154 fi
136155 done
137156 fi
0 commit comments