Created
June 12, 2012 17:58
-
-
Save timothyklim/2919040 to your computer and use it in GitHub Desktop.
Sometimes you need inline images in your single page...
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
module ApplicationHelper | |
def inline_image image_path | |
result = Rails.cache.read "InlineImageBase64::#{image_path}" | |
if result.present? | |
result | |
else | |
ext = File.extname image_path | |
image = File.open(Rails.root.join("app/assets/images/#{image_path}"), 'rb').read | |
result = "data:image/#{ext};base64,#{Base64.strict_encode64(image).strip}" | |
Rails.cache.write "InlineImageBase64::#{image_path}", result | |
result | |
end | |
end | |
end |
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
<!-- some html code --> | |
<style type="text/css"> | |
.some_class{ | |
background: url('<%= inline_image 'inline/some_image.png' %>') no-repeat top left; | |
} | |
</style> | |
<!-- some html code --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment