2018-04-26 11:42:03 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
### USAGE
|
|
|
|
|
|
|
|
# ./convertToIconset <input png> <outp iconset>
|
|
|
|
# Example
|
|
|
|
# ./convertToIconset ~/sample.png ~/Desktop/converted.iconset
|
|
|
|
|
|
|
|
# exit the shell script on error immediately
|
|
|
|
set -e
|
|
|
|
|
|
|
|
make_iconset_imagemagick() {
|
|
|
|
local file iconset
|
|
|
|
file="${1}"
|
|
|
|
iconset="${2}"
|
|
|
|
|
|
|
|
mkdir "$iconset"
|
|
|
|
|
|
|
|
for size in {16,32,64,128,256,512}; do
|
2021-02-28 19:21:28 +00:00
|
|
|
$CONVERT "${file}" -define png:big-depth=16 -define png:color-type=6 -sample "${size}x${size}" "${iconset}/icon_${size}x${size}.png"
|
|
|
|
$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"
|
2018-04-26 11:42:03 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
make_iconset_sips() {
|
|
|
|
local file iconset
|
|
|
|
file="${1}"
|
|
|
|
iconset="${2}"
|
|
|
|
|
|
|
|
mkdir "$iconset"
|
|
|
|
|
|
|
|
for size in {16,32,64,128,256,512}; do
|
|
|
|
sips --setProperty format png --resampleHeightWidth "${size}" "${size}" "${file}" --out "${iconset}/icon_${size}x${size}.png" &> /dev/null
|
|
|
|
sips --setProperty format png --resampleHeightWidth "$((size * 2))" "$((size * 2))" "${file}" --out "${iconset}/icon_${size}x${size}@2x.png" &> /dev/null
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Parameters
|
|
|
|
SOURCE="$1"
|
|
|
|
DEST="$2"
|
|
|
|
|
|
|
|
# Check source and destination arguments
|
|
|
|
if [ -z "${SOURCE}" ]; then
|
|
|
|
echo >&2 "No source image specified"; exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${DEST}" ]; then
|
|
|
|
echo >&2 "No destination specified"; exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
HAVE_IMAGEMAGICK=
|
|
|
|
HAVE_SIPS=
|
2021-02-28 19:21:28 +00:00
|
|
|
HAVE_GRAPHICSMAGICK=
|
|
|
|
CONVERT=
|
2018-04-26 11:42:03 +00:00
|
|
|
|
2021-02-28 19:21:28 +00:00
|
|
|
type gm &>/dev/null && gm version | grep GraphicsMagick &>/dev/null && HAVE_GRAPHICSMAGICK=true && CONVERT="gm convert"
|
|
|
|
type convert &>/dev/null && HAVE_IMAGEMAGICK=true && CONVERT="convert"
|
2018-04-26 11:42:03 +00:00
|
|
|
type sips &>/dev/null && HAVE_SIPS=true
|
|
|
|
|
2021-02-28 19:21:28 +00:00
|
|
|
if [[ -n "$HAVE_IMAGEMAGICK" || -n "$HAVE_GRAPHICSMAGICK" ]]; then
|
2018-04-26 11:42:03 +00:00
|
|
|
PNG_PATH="$(mktemp -d)/icon.png"
|
|
|
|
"${BASH_SOURCE%/*}/convertToPng" "${SOURCE}" "${PNG_PATH}"
|
|
|
|
make_iconset_imagemagick "${PNG_PATH}" "${DEST}"
|
2021-02-28 19:21:28 +00:00
|
|
|
elif [[ -n "$HAVE_SIPS" ]]; then
|
2018-04-26 11:42:03 +00:00
|
|
|
make_iconset_sips "${SOURCE}" "${DEST}"
|
|
|
|
else
|
|
|
|
echo >&2 "Cannot find convert or sips executables"; exit 1;
|
|
|
|
fi
|