2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 18:48:55 +00:00
nativefier/bin/convertToPng

58 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
2016-03-10 07:41:25 +00:00
# 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; }
2016-03-10 07:41:25 +00:00
# Parameters
SOURCE="$1"
DEST="$2"
2016-03-10 07:41:25 +00:00
# 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}")
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 )"
2016-03-10 07:41:25 +00:00
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