UNPKG

1.78 kBPlain TextView Raw
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
10set -e
11
12make_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
25make_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
39SOURCE="$1"
40DEST="$2"
41
42# Check source and destination arguments
43if [ -z "${SOURCE}" ]; then
44 echo >&2 "No source image specified"; exit 1
45fi
46
47if [ -z "${DEST}" ]; then
48 echo >&2 "No destination specified"; exit 1
49fi
50
51HAVE_IMAGEMAGICK=
52HAVE_SIPS=
53
54type convert &>/dev/null && HAVE_IMAGEMAGICK=true
55type sips &>/dev/null && HAVE_SIPS=true
56
57if [[ ! -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}"
61elif [[ ! -z "$HAVE_SIPS" ]]; then
62 make_iconset_sips "${SOURCE}" "${DEST}"
63else
64 echo >&2 "Cannot find convert or sips executables"; exit 1;
65fi