-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoresize.sh
executable file
·49 lines (37 loc) · 1.39 KB
/
autoresize.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
#!/bin/bash
# watches a given folder & resizes all images to a given size.
# to disable resizing, add .noresize to the filename
# also, pdfs are auto converted to png.
# depends on (ubuntu)packages 'inotify-tools' and 'imagemagick'
# use $DIRECTORY and $RESOLUTION if available, else defaults
watchdir=${DIRECTORY:-"$(pwd)/displaycontent/plakate"}
targetresolution=${RESOLUTION:-'746x'}
imgpattern='\.(png|jpg|jpeg|gif)$'
echo "watching $watchdir"
inotifywait -m -e CREATE,MOVE $watchdir --format '%f' |
while read filename; do
echo "'$filename' created!"
filepath=${watchdir}/${filename}
filenoext=${filename%.*}
# file is an image
if [[ $filename =~ .+\.(png|jpg|jpeg|gif)$ ]]; then
# file is flagged with noresize in filename
if [[ $filename =~ .+\.noresize.+ ]]; then
echo "nothing to do for '$filename'"
continue
fi
# resize image, store with ".noresize" appended
# to filename, and remove old image
echo "resizing '$filename'"
convert "$filepath" -resize $targetresolution "$watchdir/$filenoext.noresize.png"
rm -f "$filepath"
# file is a pdf
elif [[ $filename =~ .+\.pdf$ ]]; then
# convert pdf to png
echo "converting '$filename' to png"
convert -density 300 -trim "$filepath" "$watchdir/$filenoext.png"
rm -f "$filepath"
else
echo "no match for '$filename'"
fi
done