Skip to content

Instantly share code, notes, and snippets.

@Blumed
Last active March 15, 2017 15:08
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 Blumed/7583fe207c5b401a7ec6a9c63c9441cf to your computer and use it in GitHub Desktop.
Save Blumed/7583fe207c5b401a7ec6a9c63c9441cf to your computer and use it in GitHub Desktop.
Bash: Base64 CLI
#Original Script found here @puppybits https://gist.github.com/puppybits/1565441
# Example
# bas64 file.png creates the string with the needed data:image/png;base64, appended to the front of the string and copies it to your clipboard
# bas64 file.png -img does the same as above but adds an image tag around the string so all you need to do is paste into your html file
bas64(){
filename=$(basename $1)
ext=${filename##*.}
if [ $ext == gif ]; then
append="data:image/gif;base64,";
elif [ $ext == jpeg ] || [ $ext == jpg ]; then
append="data:image/jpeg;base64,";
elif [ $ext == png ]; then
append="data:image/png;base64,";
echo "this worked $1"
elif [ $ext == svg ]; then
append="data:image/svg+xml;base64,";
elif [ $ext == ico ]; then
append="data:image/vnd.microsoft.icon;base64,";
fi
#Mathias Bynens - http://superuser.com/questions/120796/os-x-base64-encode-via-command-line
data=$(openssl base64 < $1 | tr -d '\n')
if [ "$#" -eq 2 ] && [ $2 == -img ]; then
data=\<img\ src\=\"$append$data\"\ alt\=\"base64\"\ >
else
data=$append$data
fi
echo $data | pbcopy
echo "copied to clipboard"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment