-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageResize
executable file
·88 lines (69 loc) · 2.06 KB
/
imageResize
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
#
# Uses ImageMagick to downsize images
#
# Accepts: png, jpg, jpeg, gif
# Outputs: png
# Usage: ./imageResize WIDTH HEIGHT FILENAME
# Ex: ./imageResize 320 480 image.png
#
# Latest Test Environment
# OSX: 10.12.3
# ImageMagick: 7.0.5-4 Q16 x86_64
#
# =====================================
# ==========[ Library Check ]==========
# =====================================
command -v convert >/dev/null 2>&1 || {
echo >&2 'Error: cannot find "convert" command.... Is ImageMagick installed?';
exit 1;
}
# ================================================
# ==========[ Arguments Validity Check ]==========
# ================================================
if [ "$#" -ne 3 ]; then
echo "Error: Incorrect number of arguments ($#). Needs to be 3."
echo "Usage: imageResize WIDTH HEIGHT FILENAME"
echo "Example: imageResize 360 480 test.png"
exit 1
fi
width=$1
height=$2
filePath=$3
numRegExp='^[0-9]+$'
if ! [[ $width =~ $numRegExp ]] ; then
echo "Error: Width is not a valid number" >&2
exit 1
fi
if ! [[ $height =~ $numRegExp ]] ; then
echo "Error: Height is not a valid number" >&2
exit 1
fi
if [ ! -f "$filePath" ]; then
echo "File ($filePath) not found!"
exit 1
fi
fileFullName=`echo $filePath|grep -i ".png\|.jpg\|.jpeg\|.gif\|.tiff"`
if [ ! -n "$fileFullName" ] ; then
echo "File not a valid format (png,jpg,jpeg,gif,tiff)"
exit 1
fi
# ==========================================
# ==========[ Converting Process ]==========
# ==========================================
echo "File: \t\t"$fileFullName
echo "Resolution:\t""$width""x""$height"
printf "Converting:\t...."
fileName="${fileFullName%.*}"
fileExt="${fileFullName##*.}"
# 1/3 - Strip(reconvert) non-standard PNG (Left by amazing Photoshop.)
if [ $fileExt == "png" ] ; then
convert "$fileFullName" -strip "$fileFullName"
fi
# 2/3 - The actual conversion
convert "$fileName"".""$fileExt" -resize "$width""x""$height"\> "$fileName"".png"
# 3/3 - Cleanup - Original PNGs are overwritten. Remove anything not originally PNG format
if [ $fileExt != "png" ] ; then
rm $fileFullName
fi
echo " Done!"