Skip to content

Instantly share code, notes, and snippets.

@tomayac
Created June 19, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomayac/556d99891f0638909a7ac7718a93224e to your computer and use it in GitHub Desktop.
Save tomayac/556d99891f0638909a7ac7718a93224e to your computer and use it in GitHub Desktop.
Resize script for web.dev images
#!/bin/sh
set -e
maxwidth="1600" # in pixels, the widest image you want to allow.
#find all .png in current dir and subdirectories
FILES="$(find . -iname '*.png')"
for imagefile in $FILES
do
if [ -f "$imagefile" ]; then
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print $2}'`
else
echo "Oops, "$imagefile" does not exist."
exit
fi
if [ $imgwidth -gt $maxwidth ]; then
echo " - Image too big. Resizing..."
sips --resampleWidth $maxwidth "$imagefile" > /dev/null 2>&1 # to hide sips' ugly output
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print $2}'`
imgheight=`sips --getProperty pixelHeight "$imagefile" | awk '/pixelHeight/ {print $2}'`
echo " - Resized "$imagefile" to $imgwidth""px wide by $imgheight""px tall";
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment