Created
August 8, 2012 11:16
-
-
Save peteboere/3294328 to your computer and use it in GitHub Desktop.
Utilities for working with svgs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Utilities for working with svgs | |
# | |
# svg2png - Create png file from source svg using inkscape | |
# svg2pngr - Create png file from source svg using inkscape recursively | |
# svgz - Create svgz file from source svg | |
# svgzr - Create svgz file from source svg recursively | |
# | |
# Create an alias to your inkscape command line utility. | |
# Edit depending on your setup | |
alias inkscape="/Applications/Inkscape.app/Contents/Resources/script" | |
# | |
# Convert svg files to png files using inkscape | |
# | |
function svg2png { | |
local file="$1" | |
local ext=${file##*.} | |
if [[ ! -n $file ]]; then | |
echo "No file specified" | |
return | |
fi | |
if [[ $ext != "svg" ]]; then | |
echo "Input file must be an svg" | |
return | |
fi | |
# Convert the file path to absolute for inkscape | |
if [[ -f "$PWD/$file" ]]; then | |
file="$PWD/$file" | |
fi | |
if [[ ! -f "$file" ]]; then | |
echo "File not found" | |
return | |
fi | |
# Determine width of the output png. | |
# If no width argument is passed then query the input svg file for a width with inkscape | |
local width=${2} | |
if [[ ! -n "$2" ]]; then | |
width=$(inkscape -W "$file" 2>/dev/null) | |
width=$(php -r "echo round( $width );") | |
fi | |
local dir=$(dirname "$file") | |
local basename=$(basename "$file" .svg) | |
local subdir="png/${width}x" | |
local outputfile="${dir}/$subdir/${basename}.png" | |
# Create a sub directory for generated files | |
if [[ ! -d "$dir/$subdir" ]]; then | |
mkdir -p "$dir/$subdir" | |
echo "Creating directory structure" | |
fi | |
inkscape --export-png="$outputfile" --export-width=$width "$file" | |
} | |
# | |
# Recursively convert found svg files to png files | |
# | |
function svg2pngr { | |
for f in $(find . -name '*.svg'); do | |
svg2png "$f" "$1" | |
done | |
} | |
# | |
# Create svgz files from current directory svg files | |
# | |
function svgz { | |
for f in $(find . -name '*.svg'); do | |
local ext=${f##*.} | |
if [[ $ext = "svg" ]]; then | |
local outfile="$(basename $f .svg).svgz" | |
cat "$f" | gzip > "$outfile" | |
fi | |
done | |
} | |
# | |
# Recursively create svgz files from svg files starting in current directory | |
# | |
function svgzr { | |
for f in $(find . -name '*.svg'); do | |
local dir="$(dirname $f)" | |
local outfile="${dir}/$(basename $f .svg).svgz" | |
cat "$f" | gzip > "$outfile" | |
done | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment