2014-12-06 03:26:50 +11:00
|
|
|
require 'jekyll'
|
|
|
|
require 'rmagick'
|
|
|
|
|
|
|
|
module Jekyll
|
|
|
|
class ResponsiveImage
|
|
|
|
class Tag < Liquid::Tag
|
|
|
|
DEFAULT_QUALITY = 85
|
|
|
|
|
|
|
|
def initialize(tag_name, markup, tokens)
|
|
|
|
super
|
|
|
|
|
|
|
|
@attributes = {}
|
|
|
|
@markup = markup
|
|
|
|
|
|
|
|
@markup.scan(::Liquid::TagAttributes) do |key, value|
|
|
|
|
# Strip quotes from around attribute values
|
|
|
|
@attributes[key] = value.gsub(/^['"]|['"]$/, '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
def resize_image(img, config)
|
2014-12-06 03:26:50 +11:00
|
|
|
output_dir = config['output_dir']
|
|
|
|
ensure_output_dir_exists!(output_dir)
|
|
|
|
|
|
|
|
resized = []
|
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
config['sizes'].each do |size|
|
2014-12-06 03:26:50 +11:00
|
|
|
width = size['width']
|
|
|
|
ratio = width.to_f / img.columns.to_f
|
|
|
|
height = (img.rows.to_f * ratio).round
|
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
filename = resized_filename(img.filename, width, height)
|
|
|
|
filepath = "#{output_dir}/#{filename}"
|
2014-12-06 03:26:50 +11:00
|
|
|
|
|
|
|
next unless needs_resizing?(img, width)
|
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
resized.push(image_hash(filepath, width, height))
|
2014-12-06 03:26:50 +11:00
|
|
|
|
|
|
|
# Don't resize images more than once
|
2014-12-07 06:18:59 +11:00
|
|
|
next if File.exists?(filepath)
|
2014-12-06 03:26:50 +11:00
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
Jekyll.logger.info "Generating #{filepath}"
|
2014-12-06 03:26:50 +11:00
|
|
|
|
|
|
|
i = img.scale(ratio)
|
2014-12-07 06:18:59 +11:00
|
|
|
i.write(filepath) do |f|
|
2014-12-06 03:26:50 +11:00
|
|
|
f.quality = size['quality'] || DEFAULT_QUALITY
|
|
|
|
end
|
|
|
|
|
|
|
|
i.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
resized
|
|
|
|
end
|
|
|
|
|
|
|
|
# Insert resize information into a file path
|
|
|
|
#
|
|
|
|
# resized_filename(/foo/bar/file.name.jpg, 500, 300)
|
|
|
|
# => /foo/bar/file.name-500x300.jpg
|
|
|
|
#
|
|
|
|
def resized_filename(path, width, height)
|
|
|
|
File.basename(path).sub(/\.([^.]+)$/, "-#{width}x#{height}.\\1")
|
|
|
|
end
|
|
|
|
|
|
|
|
def needs_resizing?(img, width)
|
|
|
|
img.columns > width
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_output_dir_exists!(dir)
|
|
|
|
unless Dir.exists?(dir)
|
|
|
|
Jekyll.logger.info "Creating output directory #{dir}"
|
|
|
|
Dir.mkdir(dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
# Build a hash containing image information
|
|
|
|
def image_hash(path, width, height)
|
|
|
|
{
|
|
|
|
'path' => path,
|
|
|
|
'width' => width,
|
|
|
|
'height' => height,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-12-06 03:26:50 +11:00
|
|
|
def render(context)
|
|
|
|
config = context.registers[:site].config['responsive_image']
|
|
|
|
config['output_dir'] ||= 'assets/resized'
|
|
|
|
config['sizes'] ||= []
|
|
|
|
|
2014-12-07 06:18:59 +11:00
|
|
|
img = Magick::Image::read(@attributes['path']).first
|
|
|
|
@attributes['original'] = image_hash(@attributes['path'], img.columns, img.rows)
|
|
|
|
@attributes['resized'] = resize_image(img, config)
|
2014-12-07 02:33:38 +11:00
|
|
|
@attributes['template'] ||= config['template']
|
2014-12-06 03:26:50 +11:00
|
|
|
|
2014-12-07 02:33:38 +11:00
|
|
|
partial = File.read(@attributes['template'])
|
2014-12-06 03:26:50 +11:00
|
|
|
template = Liquid::Template.parse(partial)
|
|
|
|
|
|
|
|
template.render!(@attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('responsive_image', Jekyll::ResponsiveImage::Tag)
|