jekyll-responsive-image/lib/jekyll/responsive_image/image_resizer.rb

72 lines
2.0 KiB
Ruby
Raw Permalink Normal View History

2016-09-28 07:04:09 +10:00
module Jekyll
module ResponsiveImage
class ImageResizer
def self.resize(image, config)
self.new.resize(image, config)
end
def resize(image, config)
2016-09-28 07:54:07 +10:00
results = []
2016-09-28 07:04:09 +10:00
config['sizes'].each do |size|
width = size['width']
ratio = width.to_f / image.columns.to_f
height = (image.rows.to_f * ratio).round
next unless needs_resizing?(image, width)
2016-09-28 07:54:07 +10:00
image_path = image.filename
source_img = Image.new(image_path, width, height, config)
site_source_path = format_output_path(config['output_path_format'], source_img.to_h)
2016-09-28 07:04:09 +10:00
2016-09-28 07:54:07 +10:00
resized_img = Image.new(site_source_path, width, height, config)
results.push(resized_img)
2016-09-28 07:04:09 +10:00
# Don't resize images more than once
2016-09-28 07:54:07 +10:00
next if File.exist?(site_source_path)
site_dest_path = File.join(config[:site_dest], site_source_path)
ensure_output_dir_exists!(site_source_path)
ensure_output_dir_exists!(site_dest_path)
Jekyll.logger.info "Generating #{site_source_path}"
2016-09-28 07:04:09 +10:00
resized = image.scale(ratio)
2016-09-28 07:54:07 +10:00
resized.write(site_source_path) do |i|
2016-09-28 07:04:09 +10:00
i.quality = size['quality'] || config['default_quality']
end
2016-09-28 07:54:07 +10:00
# Ensure the generated file is copied to the _site directory
Jekyll.logger.info "Copying image to #{site_dest_path}"
FileUtils.copy_file(site_source_path, site_dest_path)
2016-09-28 07:04:09 +10:00
end
2016-09-28 07:54:07 +10:00
results
end
def format_output_path(format, image_hash)
params = symbolize_keys(image_hash)
Pathname.new(format % params).cleanpath.to_s
end
def symbolize_keys(hash)
hash.each_with_object({}){ |(key, val), h| h[key.to_sym] = val }
2016-09-28 07:04:09 +10:00
end
def needs_resizing?(image, width)
image.columns > width
end
2016-09-28 07:54:07 +10:00
def ensure_output_dir_exists!(path)
dir = File.dirname(path)
2016-09-28 07:04:09 +10:00
unless Dir.exist?(dir)
Jekyll.logger.info "Creating output directory #{dir}"
FileUtils.mkdir_p(dir)
end
end
end
end
end