Converting SVG to PNG online and in your terminal

This post is mostly for my own benefit on the rare occasion that I have to convert SVG files to PNG.

I always spend a large amount of time looking for an online SVG to PNG converter or a terminal one-liner to do this.

SVG icon resources:

Online services to convert SVG to PNG that aren’t totally rubbish:

Convert SVG to PNG using your terminal (Mac, Linux):

First install librsvg using brew:

$ brew update && brew upgrade
$ brew install librsvg
$ ln -s /usr/local/bin/rsvg-convert /usr/local/bin/rsvg

Then, you can either using rsvg directly, or make a function like the following:

# svg2png [file] [width] [height]
function svg2png {
    file=$1
    file_name="${file%.*}"
    file_ext="${file##*.}"
    shift
    width=${1:-"128"}
    height=${2:-"128"}

    if [ "${file_ext}" != "svg" ]; then
        printf "\n${file} is not an svg!\n"
    else
        rsvg -w $width -h $height "${file}" -o "${file_name}.png"
    fi
}

If you found this useful let me know, I for one am glad that I’ve got this information in one place now.

[Comments]

Want to comment? You can do so via Github.
Comments via Github are currently closed.

[Webmentions]

Want to reply? I've hooked up Webmentions, so give it a go!