Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Last active October 26, 2018 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/a13e5510d5a339d9d51a240859ec025c to your computer and use it in GitHub Desktop.
Save ccnokes/a13e5510d5a339d9d51a240859ec025c to your computer and use it in GitHub Desktop.
Generate a data URI for a file
data_uri() {
if [[ "$#" -ne 1 ]] || [[ ! -f $1 ]]; then
echo "ERROR! Usage: ${FUNCNAME[0]} <file name>"
return 1
fi
mime=$(file -b --mime-type "$1")
data=$(base64 "$1")
echo "data:$mime;base64,$data"
}
# sample usage
data_uri image.jpeg
# outputs: data:image/jpeg;base64,/9j/4QAYRXhp....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment