Skip to content

Instantly share code, notes, and snippets.

@xgotyou
Last active October 5, 2018 17:49
Show Gist options
  • Save xgotyou/6caf280f5bf8e63544e1ed4a7a2f70fd to your computer and use it in GitHub Desktop.
Save xgotyou/6caf280f5bf8e63544e1ed4a7a2f70fd to your computer and use it in GitHub Desktop.
Rails images for retina and other high dpi displays using srcset
module ImagesHelper
# Outputs html img tag with srcset attribute for 2x image based on original
# src. Use naming convention ex. having 'image.jpg', 2x image should be named
# 'image@2x.jpg'. If there's no image.jpg or image@2x.jpg exception will be
# thrown by Rails asset pipeline.
def retina_image(src, options = {})
src2x = src.gsub(/(^.+)(\.(jpg|png)$)/, '\1@2x\2')
image_tag src, options.merge(srcset: "#{image_url(src2x)} 2x")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment