Skip to content

Instantly share code, notes, and snippets.

@phillip-boombox
Last active August 5, 2019 17:42
Show Gist options
  • Save phillip-boombox/93aa465ee0fc3c3b0535e852e5ee11ca to your computer and use it in GitHub Desktop.
Save phillip-boombox/93aa465ee0fc3c3b0535e852e5ee11ca to your computer and use it in GitHub Desktop.
Convert an image to base64 data URI that's then copied to the clipboard.
#!/bin/bash
# Create a base64 data URI from an image and copy it to clipboard.
# Uses OpenSSL to create the base64 representation.
the_mimetype=$(file -bN --mime-type "$1")
the_content=$(openssl base64 < "$1" | tr -d '\n')
# Use printf instead of echo to avoid the ending newline
printf "data:$the_mimetype;base64,$the_content" | pbcopy
@phillip-boombox
Copy link
Author

phillip-boombox commented Aug 5, 2019

To Use

  1. Download this script into a folder that will contain images.
  2. Open a terminal window and cd into the folder.
  3. Make this script executable by typing chmod +x img2datauri.sh.
  4. Execute by typing ./img2datauri.sh image.png. Replace image.png with proper filename. Data URI can then be pasted into browser dev tools to see the new image.

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