Skip to content

Instantly share code, notes, and snippets.

@sod
Created March 14, 2023 21:34
Show Gist options
  • Save sod/d5e8941fc46ba7ad07e3f3e826522375 to your computer and use it in GitHub Desktop.
Save sod/d5e8941fc46ba7ad07e3f3e826522375 to your computer and use it in GitHub Desktop.
#!/bin/bash
EXECUTABLE_WEBP_CHECK=$(brew ls --versions webp)
if [[ "" == "${EXECUTABLE_WEBP_CHECK}" ]]; then
echo "Error: 'cwebp' missing. Please install via 'brew install webp'"
exit 1
fi
processFile() {
SOURCE="$1"
FILENAME=$(basename -- "${SOURCE}")
DIR=$(dirname -- "${SOURCE}")
FILE="${FILENAME%.*}"
(
set -x
cwebp "${SOURCE}" -o "${DIR}/${FILE}.webp"
)
}
if [ -p /dev/stdin ]; then
while IFS= read line; do
processFile "${line}"
done
else
if [ -f "$1" ]; then
processFile "${1}"
else
echo "This script creates webp from the source files"
echo
echo "Usage:"
echo
echo "Pipe:"
echo " find web/images/foo -type f | ../bin/image-generate-webp"
echo
echo "Argument:"
echo " ../bin/image-generate-webp web/images/foo/bar.jpg"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment