EN
Bash - convert png image to base64 data url
9
points
In this short article, we would like to show how to convert *.png
file to Data URL encoded with base64 using Bash.
Quick solution:
echo -n "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 inside).
Example result
Now Data URL is written to output_image.txt
can be used as an embedded resource on the web page, eg.
<html>
<body>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT ...">
</body>
</html>
Screenshot: