Skip to content

Instantly share code, notes, and snippets.

@pichfl
Created January 2, 2012 01:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pichfl/1548864 to your computer and use it in GitHub Desktop.
Save pichfl/1548864 to your computer and use it in GitHub Desktop.
Liquid Image Tag for Jekyll
# A simple plugin for Jekyll that allows you to use {% url "alt text" %} to add images to your posts.
# It will automatically check those images for width and height.
#
# Requires http://imagesize.rubyforge.org/
require 'image_size'
require 'open-uri'
module Jekyll
class ImageTag < Liquid::Tag
@img = nil
@alt = ""
def initialize(tag_name, markup, tokens)
@img = markup.strip
if temp = @img.match(/^(.*)(\s)(\")(.*)(\")$/)
@img = temp[1]
@alt = temp[4]
end
super
end
def render(context)
w,h = open(@img, "rb") do |fh|
ImageSize.new(fh.read).get_size
end
if @img
"<img src=\"#{@img}\" width=\"#{w}\" height=\"#{h}\" alt=\"#{@alt}\" >"
else
end
end
end
end
Liquid::Template.register_tag('img', Jekyll::ImageTag)
@alvarogarcia7
Copy link

Hello, when using this, I've had success if you invoke the tag

{% img url "alt text" %}

for the updated header, see https://gist.github.com/alvarogarcia7/9aebc6ca3db08a26d6378e5bc4611a96

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