diff --git a/README.md b/README.md index a81d10c..60d600b 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,10 @@ responsive_image: # output_path_format: assets/resized/%{width}/%{basename} + # [Optional, Default: true] + # Whether or not to save the generated assets into the source folder. + save_to_source: false + # [Optional, Default: []] # By default, only images referenced by the responsive_image and responsive_image_block # tags are resized. Here you can set a list of paths or path globs to resize other diff --git a/lib/jekyll-responsive-image/config.rb b/lib/jekyll-responsive-image/config.rb index fcaea4d..261035e 100644 --- a/lib/jekyll-responsive-image/config.rb +++ b/lib/jekyll-responsive-image/config.rb @@ -7,7 +7,8 @@ module Jekyll 'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}', 'sizes' => [], 'extra_images' => [], - 'auto_rotate' => false + 'auto_rotate' => false, + 'save_to_source' => true } def initialize(site) diff --git a/lib/jekyll-responsive-image/resize_handler.rb b/lib/jekyll-responsive-image/resize_handler.rb index d1bb383..bc86b68 100644 --- a/lib/jekyll-responsive-image/resize_handler.rb +++ b/lib/jekyll-responsive-image/resize_handler.rb @@ -22,23 +22,31 @@ module Jekyll site_source_filepath = File.expand_path(filepath, config[:site_source]) site_dest_filepath = File.expand_path(filepath, config[:site_dest]) - # Don't resize images more than once - next if File.exist?(site_source_filepath) + if config['save_to_source'] + target_filepath = site_source_filepath + else + target_filepath = site_dest_filepath + end - ensure_output_dir_exists!(site_source_filepath) + # Don't resize images more than once + next if File.exist?(target_filepath) + + ensure_output_dir_exists!(target_filepath) ensure_output_dir_exists!(site_dest_filepath) - Jekyll.logger.info "Generating #{site_source_filepath}" + Jekyll.logger.info "Generating #{target_filepath}" i = img.scale(ratio) - i.write(site_source_filepath) do |f| + i.write(target_filepath) do |f| f.interlace = i.interlace f.quality = size['quality'] || config['default_quality'] end - # Ensure the generated file is copied to the _site directory - Jekyll.logger.info "Copying resized image to #{site_dest_filepath}" - FileUtils.copy_file(site_source_filepath, site_dest_filepath) + if config['save_to_source'] + # Ensure the generated file is copied to the _site directory + Jekyll.logger.info "Copying resized image to #{site_dest_filepath}" + FileUtils.copy_file(site_source_filepath, site_dest_filepath) + end i.destroy! end