Skip to content

Instantly share code, notes, and snippets.

@krishan
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save krishan/308ea6a89f6fa48bd225 to your computer and use it in GitHub Desktop.

Select an option

Save krishan/308ea6a89f6fa48bd225 to your computer and use it in GitHub Desktop.
Responsive Images with Scrivito
<%= scrivito_image_tag widget, :image, {
class: 'image-widget',
srcset: srcset_for(widget.image)
}, transform: {width: 1280} %>
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