Bash - convert svg to base64 data url

7 points
Created by:

In this short article we would like to show how to convert *.svg file to Data URL encoded with base64 using Bash (under Linux or Git for Windows).

Quick solution:

echo "data:image/svg+xml;base64,$(cat input_image.svg | base64 | tr -d '\r\n')" > output_image.txt

Where: input_image.svg is converted to output_image.txt (Data URL with base64).


Now Data URL written to output_image.txt can be used as embeded resource on web page, eg.

  <img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmc ...">


SVG conversion to base64 Data URL under Bash
SVG conversion to base64 Data URL under Bash
Hey 👋
Would you like to know what we do?
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.