Skip to content

Instantly share code, notes, and snippets.

@wteuber
Forked from anonymous/gist:4317247
Last active December 9, 2015 19:28
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 wteuber/4317254 to your computer and use it in GitHub Desktop.
Save wteuber/4317254 to your computer and use it in GitHub Desktop.
#http://tools.ietf.org/html/rfc2397
img_path = '...pictures/img.jpg'
img = File.open(img_path, 'rb').read
img_type = case img[0..10]
when /^GIF8/
'gif'
when Regexp.new('^\x89PNG', nil, 'n')
'png'
when Regexp.new('^\xff\xd8\xff\xe0\x00\x10JFIF', nil, 'n'), Regexp.new('^\xff\xd8\xff\xe1(.*){2}Exif', nil, 'n')
'jpeg'
else 'unknown'
end
data64 = [img].pack('m0')
html = %(<img alt="" src="data:image/#{img_type};base64,#{data64}" />)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment