-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize-images
executable file
·125 lines (109 loc) · 2.95 KB
/
resize-images
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env sh
#
# Copyright 2017 Thomas Heinemann
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Variable initialization
SOURCE="$PWD"
TARGET="Publish"
size="2048x1536"
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
pushd "$PWD"
abort() {
popd
exit 1
}
checkSize() {
size=$1
if ! ( echo $size | grep -P "^[0-9]+x[0-9]+$" -q )
then
>&2 echo "$size is not a valid size specification."
>&2 echo "Must be of format <WIDTH>x<HEIGHT>, e.g. 2048x1536"
abort
fi
}
printHelpAndExit() {
echo "$(basename $0) - Resize several images in a batch"
echo ""
echo "Usage: $(basename $0) [-S SOURCE] [-T TARGET] [-f FILE] [-s SIZE] [-h]"
echo ""
echo "Options:"
echo " -S SOURCE Specifies the source folder to read from. Default is the current directory."
echo " -T TARGET Specifies the target directory. Default is \"Publish\""
echo " -f FILE Specifies a file to process. If not given, <TARGET>.txt (from the target name, see above)"
echo " is used if it exists; if this file does not exist, resize.txt is used. If resize.txt file does"
echo " not exist either, then all files matching *.JPG or *.jpg are processed."
echo " -f Prints this help text and exits."
echo " -s SIZE Resizes the images to the specified size. SIZE must be in the form <WIDTH>x<HEIGHT>."
echo " Default is 2048x1536."
popd
exit $1
}
while getopts f:s:S:T:vh opt
do
case $opt in
f) customFileParam=$OPTARG
customFile=$(realpath $customFileParam);;
s) size=$OPTARG
checkSize $size;;
S) SOURCE=$OPTARG;;
T) TARGET=$OPTARG;;
v) echo "Option -v is not supported yet.";;
h) printHelpAndExit 0;;
?) printHelpAndExit 1;;
esac
done
cd "$SOURCE"
if [ $? -ne 0 ]
then
echo Beende...
abort
fi
if [ ! -d "$TARGET" ]
then
mkdir "$TARGET"
if [ $? -ne 0 ]
then
echo Beende...
abort
fi
fi
if [ $customFileParam ]
then
if [ -f $customFile ]
then
FILES=$(cat $customFile)
else
>&2 echo $customFileParam does not exist or is not a file.
abort
fi
elif [ -f $TARGET.txt ]
then
FILES=$(cat $TARGET.txt)
elif [ -f resize.txt ]
then
FILES=$(cat resize.txt)
else
FILES="*.JPG *.jpg"
fi
for i in $FILES
do
echo Verkleinere $i
convert $i -resize $size $TARGET/$i
done
popd