Created
December 17, 2012 10:13
-
-
Save anonymous/4317247 to your computer and use it in GitHub Desktop.
html base64 img encoding for gif png jpg
This file contains hidden or 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
#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