Skip to content

Instantly share code, notes, and snippets.

@TurnerSoftwareDev
Created October 16, 2023 02:41
Show Gist options
  • Save TurnerSoftwareDev/a31fbc0fb7da89ee6087667bab75abdd to your computer and use it in GitHub Desktop.
Save TurnerSoftwareDev/a31fbc0fb7da89ee6087667bab75abdd to your computer and use it in GitHub Desktop.
Horizontally flip an image from a local file or a URL
#!/usr/bin/env bash
# Flips an image horizontally from either a local file or from a URL passed on the command line or to stdin,
# and prints flipped image file path to stdout.
#
# Options:
#
# -h,--help Display help
# -z,--horizontal Flip the image horizontally (default)
# -i,--input The image to flip
# -o,--output The file to output to
#
# Examples:
#
# flip-img -i ./input.png -o ./output.png
#
# Exit on first error
set -e
# Debug
#set -x
# Checks that a command is installed
installed () { command -v "${1}" >/dev/null 2>&1 || { >&2 echo "Cannot execute the ${1} command"; exit 1; } }
# Checks that a file is valid
validfile() { [[ -r ${1} ]]; }
# Checks if a variable is empty
empty () { [[ -z $1 ]]; }
# Checks that a URL is valid (attempts to fetch it)
validurl ()
{
# Read the first byte from the URL
if curl --output /dev/null --silent --fail -r 0-0 "${1}"; then
return 0
else
return 1
fi
}
help () {
scriptName=$(basename "$0")
echo "$scriptName [options]"
echo
echo Options:
echo
echo "--help, -h Prints this help menu"
echo "--input, -i The file or URL for the image to flip"
echo "--output, -o The image file to create"
echo "--horizontal, -z Flip horizontally (default)"
echo "--vertical, -v Flip vertically"
echo
echo Example:
echo
echo
echo "$scriptName" --input "./input.gif" --output "./output.gif" --horizontal
echo
}
installed convert # From Imagemagick
installed curl
outputFile=
flipOrFlop="-flop" # Default to flipping it horizontally
if [[ $# = 0 ]]; then
help
exit 1
fi
while [[ $# != 0 ]]; do
case "${1}" in
-h|--help)
help
exit 0
;;
-i|--input)
input="$2"
shift 2
;;
-o|--output)
outputFile="${2}"
shift 2
;;
-v|--vertical)
flipOrFlop="-flip"
shift
;;
-z|--horizontal)
flipOrFlop="-flop"
shift
;;
*)
shift
;;
esac
done
# Accept input name on stdin too
if [[ -z $input ]]; then
input=$(cat)
fi
# Check the parameters
if [[ -z $input ]]; then
>&2 echo "No file or URL was provided"
exit 1
fi
imgFile=
if validfile "${input}"; then
imgFile="${input}"
elif validurl "${input}"; then
tmpfile=$(mktemp /tmp/flip-img-XXXXXX)
curl --location --output "${tmpfile}" --silent "${input}"
# Add a file extension to the tmp file so the output file has the extension too.
ext=$(grep "$(file -b --mime-type "${tmpfile}")" /etc/mime.types | awk '{print $2}')
if [[ -n ${ext} ]]; then
mv "${tmpfile}" "${tmpfile}.${ext}"
tmpfile="${tmpfile}.${ext}"
fi
# Remove the tmp file when we're done.
trap 'rm -f ${tmpfile}' EXIT INT
imgFile="${tmpfile}"
else
>&2 echo "${input} cannot be read"
exit 1
fi
if empty "$outputFile"; then
outputFile=$(echo "${imgFile}" | sed -E "s|(.*)(\..*)|\1_flipped\2|")
fi
convert "${flipOrFlop}" "${imgFile}" "${outputFile}"
if [[ -r ${outputFile} ]]; then
echo "${outputFile}"
exit 0
else
>&2 echo "${outputFile} was not created"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment