Skip to content

Instantly share code, notes, and snippets.

@bf4
Created March 18, 2013 17:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/5188994 to your computer and use it in GitHub Desktop.
Save bf4/5188994 to your computer and use it in GitHub Desktop.
Image to Data URI in ruby
- img_url = "http://example.com/image.jpg"
- img_type = img_url.split('.')[-1]
- img_binary = open(img_url).read
- img_data = ActiveSupport::Base64.encode64(img_binary).gsub("\n", '')
%img{src: "data:image/#{img_type};base64,#{img_data}"}
@YaEvan
Copy link

YaEvan commented Feb 20, 2023

img_data = ActiveSupport::Base64.encode64(img_binary).gsub("\n", '') replace to img_data = Base64.strict_encode64(img_binary)

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