Skip to content

Instantly share code, notes, and snippets.

@easonhan007
Created May 25, 2013 14:28
Show Gist options
  • Save easonhan007/5649264 to your computer and use it in GitHub Desktop.
Save easonhan007/5649264 to your computer and use it in GitHub Desktop.
encode a jpg image file to base64 string and display it in an html file
require 'base64'
binary = File.open(File.join('.', 'img.jpg'), 'rb') do |io|
io.read
end
encoded = Base64.encode64(binary)
printf('<img src="data:image/jpeg;base64,%s"></img>', encoded)
# ruby encode_img.rb > img.html
# open img.html with chrome or firefox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment