Skip to content

Instantly share code, notes, and snippets.

@kamikat
Forked from puppybits/image64.sh
Last active January 31, 2024 06:08
Show Gist options
  • Save kamikat/55b46ee282baf1398987fc139a031c36 to your computer and use it in GitHub Desktop.
Save kamikat/55b46ee282baf1398987fc139a031c36 to your computer and use it in GitHub Desktop.
Create data URI image from Terminal command
#!/bin/sh
# Examples:
# ./image64.sh myImage.png
# outputs: data:image/png;base64,xxxxx
# ./image64.sh myImage.png -img
# outputs: <img src="data:image/png;base64,xxxxx">
append=""
if [[ "$1" == *.gif ]]; then
append="data:image/gif;base64,";
elif [[ "$1" == *.jpeg || "$1" == *.jpg ]]; then
append="data:image/jpeg;base64,";
elif [[ "$1" == *.png ]]; then
append="data:image/png;base64,";
elif [[ "$1" == *.svg ]]; then
append="data:image/svg+xml;base64,";
elif [[ "$1" == *.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\"\>
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