forked from nativefier/nativefier-icons
-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertToPng
executable file
·58 lines (43 loc) · 1.19 KB
/
convertToPng
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
#!/bin/sh
# USAGE
# ./convertToPng <input png or ico> <outfilename>.png
# Example
# ./convertToPng ~/sample.ico ~/Desktop/converted.png
set -e
type convert >/dev/null 2>&1 || { echo >&2 "Cannot find required ImageMagick Convert executable"; exit 1; }
type identify >/dev/null 2>&1 || { echo >&2 "Cannot find required ImageMagick Identify executable"; exit 1; }
# Parameters
SOURCE=$1
DEST=$2
# Check source and destination arguments
if [ -z "${SOURCE}" ]; then
echo "No source image specified"
exit 1
fi
if [ -z "${DEST}" ]; then
echo "No destination specified"
exit 1
fi
# File Infrastructure
NAME=$(basename "${SOURCE}")
EXT="${NAME##*.}"
BASE="${NAME%.*}"
TEMP_DIR="convert_temp"
function cleanUp() {
rm -rf "${TEMP_DIR}"
}
trap cleanUp EXIT
mkdir -p "${TEMP_DIR}"
# check if .ico is a sequence
# pipe into cat so no exit code is given for grep if no matches are found
IS_ICO_SET="$(identify ${SOURCE} | grep -e "\w\.ico\[0" | cat )"
convert "${SOURCE}" "${TEMP_DIR}/${BASE}.png"
if [ "${IS_ICO_SET}" ]; then
# extract the largest(?) image from the set
cp "${TEMP_DIR}/${BASE}-0.png" "${DEST}"
else
cp "${TEMP_DIR}/${BASE}.png" "${DEST}"
fi
rm -rf "${TEMP_DIR}"
trap - EXIT
cleanUp