Skip to content

Instantly share code, notes, and snippets.

@ntorga
Created April 4, 2019 20:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntorga/e0cb6b5a60f75cb5b01e2b30c157028e to your computer and use it in GitHub Desktop.
Save ntorga/e0cb6b5a60f75cb5b01e2b30c157028e to your computer and use it in GitHub Desktop.
Decode base64 images into media files and replace the original file with WordPress upload folder. You need to upload the images into /wp-content/uploads/ after finishing.
#!/bin/bash
file=${1}
for extension in png jpeg jpg gif bmp; do
for image in `grep -ho "data:image/${extension};base64,[A-Za-z0-9+/=]*" ${file}`; do
image_signature=$(echo ${image} | md5sum | awk '{print $1}')
echo ${image} > ${image_signature}.hash
rm -f to-replace.script
echo "s|$(cat ${image_signature}.hash)|/wp-content/uploads/${image_signature}.${extension}|g" > to-replace.script
sed -i -f to-replace.script ${file}
sed -i "s|data:image/${extension};base64,||g" ${image_signature}.hash
cat ${image_signature}.hash | base64 -d > ${image_signature}.${extension}
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment