EN
Bash - convert jpg image to base64 data url
5 points
In this short article, we would like to show how to convert *.jpg
(*.jpeg
) file to Data URL encoded with base64 using Bash.
Quick solution:
xxxxxxxxxx
1
echo -n "data:image/jpeg;base64,$(cat input_image.jpg | base64 | tr -d '\r\n')" > output_image.txt
Where: input_image.jpg
is converted to output_image.txt
(Data URL with base64 inside).
Now Data URL is written to output_image.txt
can be used as an embedded resource on the web page, eg.
xxxxxxxxxx
1
<html>
2
<body>
3
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAATU0 ...">
4
</body>
5
</html>
Screenshot:
