我正在尝试编写一个 Jekyll 插件,它计算图像的纵横比并将其包装在该大小的容器中,以避免在加载页面时重排。我正在使用fastimage来计算比率,现在它看起来像这样:
require 'fastimage'
module Jekyll
class PlaceholderImageTag < Liquid::Tag
def initialize(tag_name, markup, tokens)
super
@image_url = 'image_url'
end
def render(context)
@index = Liquid::Template.parse("{{ forloop.index | minus: 1 }}").render(context)
@base_url = Liquid::Template.parse("{{ site.#{@image_url} }}").render(context)
@src = Liquid::Template.parse("{{ page.images[#{@index}].image }}").render(context)
@size = FastImage.size("http://localhost:4000/public/images/"+@src)
@ratio = (@size[1]*1.0/@size[0])*100
placeholder = "<div class='placeholder' style='padding-bottom:#{@ratio}%'>"
placeholder += "<img src=\"#{@base_url}\/#{@src}\"/>"
placeholder += "</div>"
end
end
end
Liquid::Template.register_tag('placeholder_img', Jekyll::PlaceholderImageTag)
问题是,当我在本地(localhost:4000)构建我的站点时,FastImage 返回和错误 - 如果我将 FastImage 指向不同的本地服务器(如 MAMP)或我的生产 URL,它工作得很好,但是使用 jekyll 服务器根本失败。
是否可以在不指向单独服务器的情况下让 FastImage 工作?