Created
October 16, 2023 02:40
-
-
Save TurnerSoftwareDev/e55b86c6bb5730b36bc4edbfd20d161f to your computer and use it in GitHub Desktop.
Resize an image from a local file or URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Resizes an image from either a local file or a URL passed on the command line or to stdin, | |
# and prints the resized image file path to stdout. | |
# | |
# Options: | |
# | |
# resize-img -h | |
# | |
# Examples: | |
# | |
# resize-img -i ./input.png -o ./output.png -p "50%" | |
# | |
# resize-img -i "https://example.com/input.gif" -o ./output.gif -w 1024 | |
# | |
# echo input.png | resize-img -o ./output.png -w 1200 | |
# | |
# 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" | |
echo "--input, -i The file or URL for the image to resize" | |
echo "--output, -o The image file to create" | |
echo "--percent, -p The percentage to resize the image" | |
echo "--width, -w The width of the output image" | |
echo | |
echo Example: | |
echo | |
echo | |
echo $scriptName --intput "./input.gif" --output "./output.gif" --width 1024 | |
echo | |
} | |
installed convert # From Imagemagick | |
installed curl | |
percent=50 | |
width= | |
input= | |
outputFile= | |
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 | |
;; | |
-p|--percent) | |
percent="${2}" | |
shift 2 | |
;; | |
-w|--width) | |
width="${2}" | |
shift 2 | |
;; | |
*) | |
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 | |
# Download it to a tmp file. | |
tmpfile=$(mktemp /tmp/resize-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 [[ ! -z ${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 | |
if empty "$width"; then | |
outputFile=$(echo "${imgFile}" | sed -E "s|(.*)(\..*)|\1_${percent}_percent\2|") | |
else | |
outputFile=$(echo "${imgFile}" | sed -E "s|(.*)(\..*)|\1_${width}\2|") | |
fi | |
fi | |
if [[ ! -z ${width} ]]; then | |
convert -resize "${width}" "${imgFile}" "${outputFile}" | |
elif [[ ! -z ${percent} ]]; then | |
convert -resize "${percent}"% "${imgFile}" "${outputFile}" | |
else | |
>&2 echo "No percent or width provided" | |
fi | |
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