This repository was archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathconvertToIco
More file actions
executable file
·65 lines (53 loc) · 1.34 KB
/
convertToIco
File metadata and controls
executable file
·65 lines (53 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
# USAGE
# =====
# ./convertToIco <input.svg or input.png or input.ico> <output.ico> <square edge size (optional, defaults to 256 px)>
# Example:
# ./convertToIco ~/icon.png ~/Desktop/icon_converted.ico 16
# Exit the shell script on error immediately
set -e
# Check for dependency
# imagemagick
type convert >/dev/null 2>&1 || { echo >&2 "Cannot find required ImageMagick Convert executable"; exit 1; }
# Import script as variable
CONVERT_TO_PNG="${BASH_SOURCE%/*}/convertToPng"
# Arguments
SOURCE=$1
DEST=$2
SIZE=$3
# Check arguments
if [ -z "${SOURCE}" ]; then
echo "No source image specified"
exit 1
fi
if [ -z "${DEST}" ]; then
echo "No destination specified"
exit 1
fi
if [ -z "${SIZE}" ]; then
# Set default size to 256 px
SIZE=256
fi
# File variables
SOURCE_NAME=$(basename "${SOURCE}")
SOURCE_EXT="${SOURCE_NAME##*.}"
SOURCE_BASE="${SOURCE_NAME%.*}"
# Temp directory
TEMP_DIR=".convertToIco"
mkdir -p "${TEMP_DIR}"
# Cleanup
function cleanUp() {
rm -rf "${TEMP_DIR}"
}
trap cleanUp EXIT
# Check for file type
if [ "${SOURCE_EXT}" == "ico" ]; then
cp "${SOURCE}" "${DEST}"
exit 0
elif [ "${SOURCE_EXT}" == "svg" ] || [ "${SOURCE_EXT}" == "png" ]; then
${CONVERT_TO_PNG} "${SOURCE}" "${TEMP_DIR}/${SOURCE_BASE}.png" "${SIZE}"
convert "${TEMP_DIR}/${SOURCE_BASE}.png" "${DEST}"
else
echo "SVG, PNG or ICO must be provided"
exit 1
fi