Skip to content

Instantly share code, notes, and snippets.

@SmartArray
Last active December 1, 2020 04:54
Show Gist options
  • Save SmartArray/97e3d0e0114f8ecf468c633f0b702fe4 to your computer and use it in GitHub Desktop.
Save SmartArray/97e3d0e0114f8ecf468c633f0b702fe4 to your computer and use it in GitHub Desktop.
Generate Icons from SVG for Android and iOS
#!/bin/sh
# Author: Yoshi Jaeger
# License: MIT
#
# Description: This script converts an svg file to all required sizes
# for iOS and Android. Two directories will be created next to
# the icon (ios/ and android/) in which the generated files will
# reside. Be careful and ensure these directories do not exist
# prior executing this script.
#
# Usage: ./generate_icons_from_svg.sh <input_file.svg> <1x_width> <1x_height>
#
set -e
filename="$1"
if [ ! -f `which rsvg-convert` ]; then
echo "rsvg-convert must be installed on your system"
exit 1
fi
if [ "${filename##*.}" != "svg" ]; then
echo "Input must be svg"
exit 2
fi
if [ ! -f "$filename" ]; then
echo "Input file not found"
exit 3
fi
baseDir=`dirname "$filename"`
filenameNoExt=`basename "$filename" .svg`
outputDirIos="${baseDir}/ios"
outputDirAndroid="${baseDir}/android"
echo "Creating directory $outputDirIos ..."
mkdir -p "$outputDirIos"
echo "Creating iOS icons ..."
rsvg-convert -w $2 -h $3 "$filename" > "${outputDirIos}/${filenameNoExt}.png"
rsvg-convert -w $((2*$2)) -h $((2*$3)) "$filename" > "${outputDirIos}/${filenameNoExt}@2x.png"
rsvg-convert -w $((3*$2)) -h $((3*$3)) "$filename" > "${outputDirIos}/${filenameNoExt}@3x.png"
echo "Creating directory $outputDirAndroid and subdirectories ..."
mkdir -p "${outputDirAndroid}/drawable-mdpi" \
"${outputDirAndroid}/drawable-hdpi" \
"${outputDirAndroid}/drawable-xhdpi" \
"${outputDirAndroid}/drawable-xxhdpi" \
"${outputDirAndroid}/drawable-xxxhdpi"
echo "Creating Android icons ..."
rsvg-convert -w $2 -h $3 "$filename" > "${outputDirAndroid}/drawable-mdpi/${filenameNoExt}.png"
rsvg-convert -w $((3*$2/2)) -h $((3*$3/2)) "$filename" > "${outputDirAndroid}/drawable-hdpi/${filenameNoExt}.png"
rsvg-convert -w $((2*$2)) -h $((2*$3)) "$filename" > "${outputDirAndroid}/drawable-xhdpi/${filenameNoExt}.png"
rsvg-convert -w $((3*$2)) -h $((3*$3)) "$filename" > "${outputDirAndroid}/drawable-xxhdpi/${filenameNoExt}.png"
rsvg-convert -w $((4*$2)) -h $((4*$3)) "$filename" > "${outputDirAndroid}/drawable-xxxhdpi/${filenameNoExt}.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment