Skip to content

Instantly share code, notes, and snippets.

@st44100
Created September 20, 2011 02:32
Show Gist options
  • Save st44100/1228172 to your computer and use it in GitHub Desktop.
Save st44100/1228172 to your computer and use it in GitHub Desktop.
Base64 encoding sh for PNG,GIF image.
#!/bin/sh
HEADER_STR=""
if [ $# -eq 0 ] ; then
echo "usage: ${0} [IMAGE_FILE_PATH]..."
exit 1
fi
for I in $@ ; do
HEADER_STR=`/usr/bin/file -bI ${I} | /usr/bin/sed "s# .*##g"`
if [ "${HEADER_STR}" != "image/png;" -a "${HEADER_STR}" != "image/gif;" ] ; then
echo "${I} : NOT PNG or GIF file"
continue
fi
HEADER_STR="data:"${HEADER_STR}"base64,"
echo "${HEADER_STR}" > ${I}_base64_tmp
/bin/cat ${I} | /usr/bin/openssl enc -base64 >> ${I}_base64_tmp
/bin/cat ${I}_base64_tmp | /usr/bin/tr -d '\r' | /usr/bin/tr -d '\n' > ${I}_base64
/bin/rm -f ${I}_base64_tmp
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment