Skip to content

Instantly share code, notes, and snippets.

@query
Created March 2, 2013 00:36
Show Gist options
  • Save query/5069053 to your computer and use it in GitHub Desktop.
Save query/5069053 to your computer and use it in GitHub Desktop.
Simple nanoc handling of width and height for image items, using the "dimensions" Ruby gem.
# Put this in the lib/ directory.
require 'dimensions'
def image_tag(item, attributes={})
"<img src='#{item.path}' width='#{item[:img_width]}' height='#{item[:img_height]}' alt='#{attributes[:alt]}'" + (attributes[:title] ? " title='#{attributes[:title]}'" : "") + " />"
end
preprocess do
@items.each do |item|
if %w{png gif jpg jpeg}.include? item[:extension]
dimensions = Dimensions.dimensions(item.raw_filename)
item[:img_width] = dimensions[0]
item[:img_height] = dimensions[1]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment