Skip to content

Instantly share code, notes, and snippets.

@timothyklim
Created June 12, 2012 17:58
Show Gist options
  • Save timothyklim/2919040 to your computer and use it in GitHub Desktop.
Save timothyklim/2919040 to your computer and use it in GitHub Desktop.
Sometimes you need inline images in your single page...
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
<!-- 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