From ba17ce94f0670e40fedf4ef05159abac56545e66 Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 27 Sep 2016 22:54:07 +0100 Subject: [PATCH] oh man this is getting hard --- lib/jekyll/responsive_image/image.rb | 27 +++++++++---- .../responsive_image/image_processor.rb | 2 +- lib/jekyll/responsive_image/image_resizer.rb | 40 +++++++++++++++---- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/lib/jekyll/responsive_image/image.rb b/lib/jekyll/responsive_image/image.rb index b6e110b..808ed0e 100644 --- a/lib/jekyll/responsive_image/image.rb +++ b/lib/jekyll/responsive_image/image.rb @@ -1,21 +1,32 @@ +require 'pathname' + module Jekyll module ResponsiveImage class Image - def initialize(width, height, config) + def initialize(path, width, height, config) + @path = path.force_encoding(Encoding::UTF_8) @width = width @height = height @config = config end + # The directory name, relative to base_path + def dirname + base_path = Pathname.new(File.join(@config[:site_source], @config['base_path'])) + image_path = Pathname.new(File.join(@config[:site_source], @path)) + + image_path.relative_path_from(base_path).dirname.to_s.delete('.') + end + def to_h { - 'path' => '', - 'width' => @width, - 'height' => @height, - 'basename' => '', - 'dirname' => '', - 'filename' => '', - 'extension' => '' + 'basename' => File.basename(@path), + 'dirname' => dirname, + 'extension' => File.extname(@path).delete('.'), + 'filename' => File.basename(@path, '.*'), + 'height' => @height, + 'path' => @path, + 'width' => @width } end diff --git a/lib/jekyll/responsive_image/image_processor.rb b/lib/jekyll/responsive_image/image_processor.rb index d59e387..7020146 100644 --- a/lib/jekyll/responsive_image/image_processor.rb +++ b/lib/jekyll/responsive_image/image_processor.rb @@ -11,7 +11,7 @@ module Jekyll image = Magick::Image::read(path).first { - original: Image.new, + original: Image.new(image.filename, image.columns, image.rows, config), resized: ImageResizer.resize(image, config), } end diff --git a/lib/jekyll/responsive_image/image_resizer.rb b/lib/jekyll/responsive_image/image_resizer.rb index 4d46335..89b07fd 100644 --- a/lib/jekyll/responsive_image/image_resizer.rb +++ b/lib/jekyll/responsive_image/image_resizer.rb @@ -6,7 +6,7 @@ module Jekyll end def resize(image, config) - resized = [] + results = [] config['sizes'].each do |size| width = size['width'] @@ -15,28 +15,52 @@ module Jekyll next unless needs_resizing?(image, width) - image_path = image.filename.force_encoding(Encoding::UTF_8) - output_path = image_path + 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) - resized.push(Image.new(width, height, config)) + resized_img = Image.new(site_source_path, width, height, config) + results.push(resized_img) # Don't resize images more than once - next if File.exist?(output_path) + 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}" resized = image.scale(ratio) - resized.write(output_path) do |i| + resized.write(site_source_path) do |i| i.quality = size['quality'] || config['default_quality'] end + + # 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) end - resized + 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 } end def needs_resizing?(image, width) image.columns > width end - def ensure_output_dir_exists!(dir) + def ensure_output_dir_exists!(path) + dir = File.dirname(path) + unless Dir.exist?(dir) Jekyll.logger.info "Creating output directory #{dir}" FileUtils.mkdir_p(dir)