Languages
[Edit]
EN

Bash - convert png to base64 data url

9 points
Created by:
AnnLen
12870

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

Quick solution:

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

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

Result

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

<html>
<body>
  <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT ...">
</body>
</html>

Screenshot:

PNG conversion to base64 Data URL under Bash
PNG conversion to base64 Data URL under Bash

Bash

Bash - convert png to base64 data url
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join