UNPKG

1.22 kBPlain TextView Raw
1#!/usr/bin/env bash
2
3# USAGE
4
5# ./convertToPng <input png or ico> <outfilename>.png
6# Example
7# ./convertToPng ~/sample.ico ~/Desktop/converted.png
8
9set -e
10
11type convert >/dev/null 2>&1 || { echo >&2 "Cannot find required ImageMagick Convert executable"; exit 1; }
12type identify >/dev/null 2>&1 || { echo >&2 "Cannot find required ImageMagick Identify executable"; exit 1; }
13
14# Parameters
15SOURCE="$1"
16DEST="$2"
17
18# Check source and destination arguments
19if [ -z "${SOURCE}" ]; then
20 echo "No source image specified"
21 exit 1
22fi
23
24if [ -z "${DEST}" ]; then
25 echo "No destination specified"
26 exit 1
27fi
28
29# File Infrastructure
30NAME=$(basename "${SOURCE}")
31BASE="${NAME%.*}"
32TEMP_DIR="convert_temp"
33
34function cleanUp() {
35 rm -rf "${TEMP_DIR}"
36}
37
38trap cleanUp EXIT
39
40mkdir -p "${TEMP_DIR}"
41
42# check if .ico is a sequence
43# pipe into cat so no exit code is given for grep if no matches are found
44IS_ICO_SET="$(identify "${SOURCE}" | grep -e "\w\.ico\[0" | cat )"
45
46convert "${SOURCE}" "${TEMP_DIR}/${BASE}.png"
47if [ "${IS_ICO_SET}" ]; then
48 # extract the largest(?) image from the set
49 cp "${TEMP_DIR}/${BASE}-0.png" "${DEST}"
50else
51 cp "${TEMP_DIR}/${BASE}.png" "${DEST}"
52fi
53
54rm -rf "${TEMP_DIR}"
55
56trap - EXIT
57cleanUp