14

There are online tools like duri.me that allow to create a Data URI from an image file. Are there any tools that run locally on Linux to do the same?

orschiro
  • 975
  • 2
  • 14
  • 25

1 Answers1

22

Data URIs are usually just base64-encoded with the MIME-type stuck at the front. I use this homegrown script, which takes the filename as its first and only parameter and returns the data uri, already wrapped in url('data:'):

[0 1026 8:29:38] ~ % cat $(which cssify.sh)
#!/bin/sh
mimetype=$(file -bN --mime-type "$1")
content=$(base64 -w0 < "$1")
echo "url('data:$mimetype;base64,$content')"
Ulrich Schwarz
  • 15,669
  • 4
  • 47
  • 58
  • I needed to replace `-w0` with `-b0` – thanksd Dec 12 '18 at 23:30
  • On BSD (macOS) use `base64` without parameters as it doesn't break the string by default. Code: `#!/usr/bin/env bash` `echo "url('data:$(file -bN --mime-type "$1");base64,$(base64 < "$1")')"` – iolsmit Jul 31 '19 at 11:10