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

60 lines
1.7 KiB
Ruby
Raw Normal View History

2014-12-08 00:06:28 +11:00
module Jekyll
module ResponsiveImage
2014-12-08 00:06:28 +11:00
class ResizeHandler
include ResponsiveImage::Utils
2014-12-08 00:06:28 +11:00
def resize_image(img, config)
resized = []
config['sizes'].each do |size|
width = size['width']
ratio = width.to_f / img.columns.to_f
height = (img.rows.to_f * ratio).round
next unless needs_resizing?(img, width)
image_path = img.filename.force_encoding(Encoding::UTF_8)
filepath = format_output_path(config['output_path_format'], config['base_path'], image_path, width, height)
resized.push(image_hash(config['base_path'], filepath, width, height))
2014-12-08 00:06:28 +11:00
site_source_filepath = File.expand_path(filepath, config[:site_source])
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
2014-12-08 00:06:28 +11:00
# Don't resize images more than once
next if File.exist?(site_source_filepath)
2014-12-08 00:06:28 +11:00
ensure_output_dir_exists!(File.dirname(site_source_filepath))
ensure_output_dir_exists!(File.dirname(site_dest_filepath))
2014-12-08 00:06:28 +11:00
Jekyll.logger.info "Generating #{filepath}"
i = img.scale(ratio)
i.write(site_source_filepath) do |f|
2014-12-08 00:06:28 +11:00
f.quality = size['quality'] || config['default_quality']
end
# Ensure the generated file is copied to the _site directory
FileUtils.copy_file(site_source_filepath, site_dest_filepath)
2014-12-08 00:06:28 +11:00
i.destroy!
end
img.destroy!
2014-12-08 00:06:28 +11:00
resized
end
def needs_resizing?(img, width)
img.columns > width
end
def ensure_output_dir_exists!(dir)
unless Dir.exist?(dir)
2014-12-08 00:06:28 +11:00
Jekyll.logger.info "Creating output directory #{dir}"
FileUtils.mkdir_p(dir)
end
end
end
end
end