Last active
August 29, 2015 14:26
-
-
Save krishan/308ea6a89f6fa48bd225 to your computer and use it in GitHub Desktop.
Responsive Images with Scrivito
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
| <%= scrivito_image_tag widget, :image, { | |
| class: 'image-widget', | |
| srcset: srcset_for(widget.image) | |
| }, transform: {width: 1280} %> |
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
| module SrcsetHelper | |
| # polyfill for browsers that do not support srcset: | |
| # https://scottjehl.github.io/picturefill/ | |
| def srcset_for(obj) | |
| image = obj.try(:binary) | |
| return unless image | |
| [160, 320, 640, 1280, 2560].map do |width| | |
| transformed_image = image.transform(width: width) | |
| url = scrivito_url(transformed_image) | |
| "#{url} #{width}w" | |
| end.join(", ") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment