Last active
October 26, 2018 05:07
-
-
Save ccnokes/a13e5510d5a339d9d51a240859ec025c to your computer and use it in GitHub Desktop.
Generate a data URI for a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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