Last active
October 5, 2018 17:49
-
-
Save xgotyou/6caf280f5bf8e63544e1ed4a7a2f70fd to your computer and use it in GitHub Desktop.
Rails images for retina and other high dpi displays using srcset
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 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