1 | #!/usr/bin/env bash
|
2 |
|
3 | ### USAGE
|
4 |
|
5 | # ./convertToIconset <input png> <outp iconset>
|
6 | # Example
|
7 | # ./convertToIconset ~/sample.png ~/Desktop/converted.iconset
|
8 |
|
9 | # exit the shell script on error immediately
|
10 | set -e
|
11 |
|
12 | make_iconset_imagemagick() {
|
13 | local file iconset
|
14 | file="${1}"
|
15 | iconset="${2}"
|
16 |
|
17 | mkdir "$iconset"
|
18 |
|
19 | for size in {16,32,64,128,256,512}; do
|
20 | convert "${file}" -define png:big-depth=16 -define png:color-type=6 -sample "${size}x${size}" "${iconset}/icon_${size}x${size}.png"
|
21 | convert "${file}" -define png:big-depth=16 -define png:color-type=6 -sample "$((size * 2))x$((size * 2))" "${iconset}/icon_${size}x${size}@2x.png"
|
22 | done
|
23 | }
|
24 |
|
25 | make_iconset_sips() {
|
26 | local file iconset
|
27 | file="${1}"
|
28 | iconset="${2}"
|
29 |
|
30 | mkdir "$iconset"
|
31 |
|
32 | for size in {16,32,64,128,256,512}; do
|
33 | sips --setProperty format png --resampleHeightWidth "${size}" "${size}" "${file}" --out "${iconset}/icon_${size}x${size}.png" &> /dev/null
|
34 | sips --setProperty format png --resampleHeightWidth "$((size * 2))" "$((size * 2))" "${file}" --out "${iconset}/icon_${size}x${size}@2x.png" &> /dev/null
|
35 | done
|
36 | }
|
37 |
|
38 | # Parameters
|
39 | SOURCE="$1"
|
40 | DEST="$2"
|
41 |
|
42 | # Check source and destination arguments
|
43 | if [ -z "${SOURCE}" ]; then
|
44 | echo >&2 "No source image specified"; exit 1
|
45 | fi
|
46 |
|
47 | if [ -z "${DEST}" ]; then
|
48 | echo >&2 "No destination specified"; exit 1
|
49 | fi
|
50 |
|
51 | HAVE_IMAGEMAGICK=
|
52 | HAVE_SIPS=
|
53 |
|
54 | type convert &>/dev/null && HAVE_IMAGEMAGICK=true
|
55 | type sips &>/dev/null && HAVE_SIPS=true
|
56 |
|
57 | if [[ ! -z "$HAVE_IMAGEMAGICK" ]]; then
|
58 | PNG_PATH="$(mktemp -d)/icon.png"
|
59 | "${BASH_SOURCE%/*}/convertToPng" "${SOURCE}" "${PNG_PATH}"
|
60 | make_iconset_imagemagick "${PNG_PATH}" "${DEST}"
|
61 | elif [[ ! -z "$HAVE_SIPS" ]]; then
|
62 | make_iconset_sips "${SOURCE}" "${DEST}"
|
63 | else
|
64 | echo >&2 "Cannot find convert or sips executables"; exit 1;
|
65 | fi
|