aboutsummaryrefslogtreecommitdiffstats
path: root/build/image-optimization.sh
blob: 6d494b8ddb7def71264dcc919f5935ece5bde3c9 (plain)
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
#!/usr/bin/env bash

CHECK_DIR='../'
if [[ -d "$1" ]]; then
	CHECK_DIR=$1
fi

function recursive_optimize_images() {
	cd "$1" || return
	DIR_NAME=${PWD##*/}

	if [[ "$DIR_NAME" == "node_modules" ]]; then
		return
	elif [[ "$DIR_NAME" == "tests" ]]; then
		return
	fi

	# Optimize all PNGs
	for png in *.png
	do
		[[ -e "$png" ]] || break

		optipng -o6 -strip all "$png"
	done

	# Optimize all JPGs
	for jpg in *.jpg
	do
		[[ -e "$jpg" ]] || break

		jpegoptim --strip-all "$jpg"
	done

	# Optimize all SVGs
	for svg in *.svg
	do
		[[ -e "$svg" ]] || break

		mv $svg $svg.opttmp
		scour --create-groups \
			--enable-id-stripping \
			--enable-comment-stripping \
			--shorten-ids \
			--remove-metadata \
			--strip-xml-prolog \
			--no-line-breaks  \
			-i $svg.opttmp \
			-o $svg
		rm $svg.opttmp
	done

	# Check all subfolders
	for dir in */
	do
		[[ -e "$dir" ]] || break

		if [[ -d "$dir" ]]; then
			recursive_optimize_images "$dir"
			cd ..
		fi
	done
}

recursive_optimize_images "$CHECK_DIR"