Last active
April 8, 2018 12:29
-
-
Save mikka2061/e8ddb2566d90b00f990d6a39b0fd1346 to your computer and use it in GitHub Desktop.
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
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) |
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
Hi @mikka2061,
have you deployed this code in the real world? I had a similar piece back in time for middleman.
https://github.com/rriemann/blog.riemann.cc/blob/latest-duocolor/lib/flickr_helpers.rb
I would be interested in having caching to get rid of API requested for repeated builds. Have you thought about this?