Created
February 16, 2024 14:39
-
-
Save jaydorsey/eb2594dcc37070a07e5c96fd8a6c4b7c to your computer and use it in GitHub Desktop.
Storing images as base64 representations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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