This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
srcset.sh
executable file
·201 lines (166 loc) · 5.16 KB
/
srcset.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/sh
usage() {
echo "usage: $0 [-hmz] [-q quality] [—t type] [-l legacysize] [-s sizes] [-o outdirectory] [-f filename] filename"
echo "usage: $0 [-hmz] [—n findname] [-q quality] [—t type] [-l legacysize] [-s sizes] [-o outpath] [-f filepath] filepath"
}
convert_file() {
# path and no filenames
path=${filename%/*}
# path and filename with no extension or type
pathfile=${filename%.*}
# extension or type (jpg, png, etc) either from current file OR from -t option
if ! [ -z "$desttype" ] ; then
type="$desttype"
else
type="${filename##*.}"
fi
# if out directory is empty then create file in same directory as file
if [ -z "$outdir" ] ; then
outprefix="$pathfile"
else
# filename with no path
nopath="${filename##*/}"
outprefix="$outdir/$pathfile"
# make a directory with all parents if needed -- unless running a test
if [ -z "$istest" ] ; then
mkdir -p "$outdir/$path"
fi
fi
# change convert options as needed. recommend that psd files use -flatten
# uuse convert defalt quality that is the best possible
if ! [ -z "$isinterlace" ] ; then
options="-strip -interlace Plane"
fi
if ! [ -z "$quality" ] ; then
options="$options -quality $quality"
fi
echo "Resizing $filename"
if [ -z "$istest" ] ; then
# Various resizes
convert $options -adaptive-resize 320 "$filename" "$outprefix-320w.$type"
convert $options -adaptive-resize 480 "$filename" "$outprefix-480w.$type"
convert $options -adaptive-resize 640 "$filename" "$outprefix-640w.$type"
convert $options -adaptive-resize 768 "$filename" "$outprefix-768w.$type"
convert $options -adaptive-resize 960 "$filename" "$outprefix-960w.$type"
convert $options -adaptive-resize 1024 "$filename" "$outprefix-1024w.$type"
convert $options -adaptive-resize 1280 "$filename" "$outprefix-1280w.$type"
convert $options -adaptive-resize 1440 "$filename" "$outprefix-1440w.$type"
fi
if [ -z "$legacysize" ] ; then
# We need to make a copy of the original with no resize
srcsuffix="-srcw"
if [ -z "$istest" ] ; then
convert $options "$filename" "$outprefix-srcw.$type"
fi
else
srcsuffix="-$legacysize"w
fi
outprefix="$prefix$outprefix"
str='<img src="'$outprefix$srcsuffix.$type'" srcset="'$outprefix-320w.$type' 320w, '$outprefix-480w.$type' 480w, '$outprefix-640w.$type' 640w, '$outprefix-768w.$type' 768w, '$outprefix-960w.$type' 960w, '$outprefix-1024w.$type' 1024w, '$outprefix-1440w.$type' 1440w" sizes="'$sizes'" alt="An image named '$filename'"/>'
# if 'save' argument then save to file
if [ -z "$ismarkup" ] || ! [ -z "$istest" ] ; then
echo "$str"
else
echo "$str" > "$outprefix-srcset.html"
echo "more $outprefix- srcset.html"
fi
}
find_files() {
find "$dirname" -type f -atime +1s \( -name "$pattern" -a ! -name "*-320w.*" -a ! -name "*-480w.*" -a ! -name "*-640w.*" -a ! -name "*-768w.*" -a ! -name "*-960w.*" -a ! -name "*-1024w.*" -a ! -name "*-1280w.*" -a ! -name "*-1440w.*" ! -name "*-srcw.*" \) -exec "$0" -q "$quality" -p "$prefix" -t "$desttype" -o "$outdir" -l "$legacysize" -s "$sizes" $isinterlace $ismarkup $istest {} \;
}
# —————————————————————————————— main ——————————————————————————————
dirname=""
filename=""
outdir=""
quality=""
legacysize=""
ismarkup=""
istest=""
isinterlace=""
pattern="*.jpg"
desttype=""
sizes="(min-width: 768px) 50vw, 100vw"
prefix=""
# now get options
while getopts ":f:n:p:q:o:s:t:l:hmzi" option; do
case "$option" in
f)
tmp="$OPTARG"
;;
n)
pattern="$OPTARG"
;;
p)
prefix="$OPTARG"
;;
q)
quality="$OPTARG"
;;
o)
outdir="$OPTARG"
# strip any trailing slash from the dir_name value
outdir="${outdir%/}"
;;
s)
sizes="$OPTARG"
;;
m)
# This trick is used for rercusive shells (see the find arguments)
ismarkup="-$option"
;;
z)
istest="-$option"
;;
i)
isinterlace="-$option"
;;
t)
desttype="$OPTARG"
;;
l)
legacysize="$OPTARG"
;;
h)
usage
exit 0
;;
:)
echo "Error: -$OPTARG requires an argument" >&2
usage
exit 1
;;
\?)
echo "Error: unknown option -$OPTARG" >&2
usage
exit 1
;;
esac
done
# LAST parameter *may* to be file or directory. if none specifed (using -f) then use last argument
if [ -z "$tmp" ] ; then
isfile="${!#}"
else
isfile="$tmp"
fi
# need either filename or dir so error
if [ -z "$isfile" ] || [ -b "$isfile" ] || [ -c "$isfile" ] || ! [ -e "$isfile" ] ; then
echo "Error: Needs valid filename or directory as argument" >&2
usage
exit 1
fi
# if filename is not empty then we convert a single file
if [ -f "$isfile" ]; then
filename="$isfile"
convert_file
exit 1
fi
if [ -d "$isfile" ] ; then
dirname="$isfile"
# strip any trailing slash from the filename value
dirname="${dirname%/}"
find_files
exit 1
fi
echo 'File or directory is required $isfile' >&2
usage
exit 1