Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
Created February 16, 2024 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydorsey/eb2594dcc37070a07e5c96fd8a6c4b7c to your computer and use it in GitHub Desktop.
Save jaydorsey/eb2594dcc37070a07e5c96fd8a6c4b7c to your computer and use it in GitHub Desktop.
Storing images as base64 representations
require 'base64'
file = File.open('path/to/file.ext', 'rb') # Read the image as binary
base64 = Base64.strict_encode64(file.read) # This gives you a string you can assign to a constant/variable
tmpfile = Tempfile.new(Base64.strict_decode64(base64)) # Decode and create a temporary file
Digest::MD5.file(tmpfile).base64digest # This is one way to create a checksum on the file
File.size(tmpfile) # You can also get a file size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment