Skip to content

Instantly share code, notes, and snippets.

@mikka2061
Last active April 8, 2018 12:29
Show Gist options
  • Save mikka2061/e8ddb2566d90b00f990d6a39b0fd1346 to your computer and use it in GitHub Desktop.
Save mikka2061/e8ddb2566d90b00f990d6a39b0fd1346 to your computer and use it in GitHub Desktop.
require 'flickraw'
module Jekyll
class FlickresponsiveTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
FlickRaw.api_key=Jekyll.configuration({})['flickr']['api_key']
FlickRaw.shared_secret=Jekyll.configuration({})['flickr']['secret']
end
def get_sizes(image)
i = image.tr('"','').strip()
Jekyll.logger.info("Image: '"+i+"'")
sizes = flickr.photos.getSizes :photo_id => i.to_s
s = Hash.new()
q = Hash.new()
s[:small] = sizes.find { |s| s.width == '320' }
s[:medium_1] = sizes.find { |s| s.width == '500' }
s[:medium_2] = sizes.find { |s| s.width == '640' }
s[:medium_3] = sizes.find { |s| s.width == '800' }
s[:large_1] = sizes.find { |s| s.width == '1024' }
s[:large_2] = sizes.find { |s| s.width == '1600' }
s[:large_3] = sizes.find { |s| s.width == '2048' }
s[:original] = sizes.find { |s| s.label == 'Original' }
q = Hash.new()
q[:photo_id] = i
q[:data] = s
return q
end
def render(context)
sizes = get_sizes(@text)
'<picture>
<source media="(min-width: 800px)" srcset="'+sizes[:data][:large_1]["source"].to_s+'">
<source media="(min-width: 300px)" srcset="'+sizes[:data][:medium_3]["source"].to_s+'">
<img src="'+sizes[:data][:large_2]["source"].to_s+'" style="width:auto;">
</picture>
</a>'
end
end
end
Liquid::Template.register_tag('flickresponsive', Jekyll::FlickresponsiveTag)
@rriemann
Copy link

rriemann commented Apr 8, 2018

I found some time to sit down to come up with a solution with caching https://github.com/rriemann/jekyll-flickr .

Why did you chose the picture tag and not the img srcset solution?

Best,
Robert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment