Last active
March 15, 2017 15:08
-
-
Save Blumed/7583fe207c5b401a7ec6a9c63c9441cf to your computer and use it in GitHub Desktop.
Bash: Base64 CLI
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
#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