-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize-image
More file actions
executable file
·31 lines (26 loc) · 1.12 KB
/
resize-image
File metadata and controls
executable file
·31 lines (26 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
[ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "help" ] && printf "Usage: resize-image <OutputDir> [Size] <Inputfile>\nResize images and gifs at reasonable quality\n\nSize format: #x# or #\nSize must be integer\n" && exit
[ -z "$1" ] && printf "No Directory Specified\n" 2>&1 && exit
[ "${1: -1}" == "/" ] && outputDIR=${1%?} || outputDIR=$1
shift
aspect=""
extent=""
if printf "%s\n" "$1" | grep -Eoq "[0-9]+x[0-9]+$"; then
aspect=" -scale $1"
extent=" -extent $1"
elif printf "%s\n" "$1" | grep -Eoq "^[0-9]+(x)?$"; then
aspect=" -scale $1x$1"
extent=" -extent $1x$1"
else
printf "Invalid size!\n" 2>&1
exit 1
fi
shift
[ -z "$1" ] && printf "No Input File Specified\n" 2>&1 && exit
fileList=("$@")
lenFL=${#fileList[@]}
for (( i=0; i<"$lenFL"; i++ )); do
printf "%s %s %s %s %s %s" "Resizing" "${fileList[$i]}" "to" "$(printf "%s" "$aspect" | awk '{print $2}')" "size and storing it at" "$outputDIR/${fileList[$i]}"
convert -coalesce $aspect -gravity center -background "rgba(0,0,0,0)" -compose copy $extent "${fileList[$i]}" "$outputDIR/${fileList[$i]}"
printf '\033[2K\033[0G'
done