Skip to content

Instantly share code, notes, and snippets.

@thornbill
Last active August 29, 2015 14:14
Show Gist options
  • Save thornbill/7e49ddee074bcd969328 to your computer and use it in GitHub Desktop.
Save thornbill/7e49ddee074bcd969328 to your computer and use it in GitHub Desktop.
Convert a jpg, png or gif to a data-uri
#!/bin/bash
for file;
do
echo -e "\033[0;32m>> $file\033[0m"
if [ -e $file ]; then
_ext=${file##*.}
if [[ "gif png jpg jpeg" =~ $_ext ]]; then
if [ $_ext == "jpg" ]; then _ext="jpeg"; fi
php -r "echo(\"data:image/$_ext;base64,\".base64_encode(file_get_contents(\"$file\")).\"\\n\");"
else
echo -e "\033[0;31mInvalid filetype [.$_ext]\033[0m"
fi
else
echo -e "\033[0;31mFile not found\033[0m"
fi
done
@thornbill
Copy link
Author

Usage

./to_data_uri.sh file1 file2 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment