Class: Jekyll::ResponsiveImage::ResizeHandler
- Inherits:
-
Object
- Object
- Jekyll::ResponsiveImage::ResizeHandler
- Includes:
- Utils
- Defined in:
- lib/jekyll-responsive-image/resize_handler.rb
Instance Method Summary collapse
- #ensure_output_dir_exists!(path) ⇒ Object
- #format_output_path(format, config, image_path, width, height) ⇒ Object
- #needs_resizing?(img, width) ⇒ Boolean
- #resize_image(img, config) ⇒ Object
Methods included from Utils
#image_hash, #keep_resized_image!, #relative_dirname, #symbolize_keys
Instance Method Details
#ensure_output_dir_exists!(path) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/jekyll-responsive-image/resize_handler.rb', line 72 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) end end |
#format_output_path(format, config, image_path, width, height) ⇒ Object
62 63 64 65 66 |
# File 'lib/jekyll-responsive-image/resize_handler.rb', line 62 def format_output_path(format, config, image_path, width, height) params = symbolize_keys(image_hash(config, image_path, width, height)) Pathname.new(format % params).cleanpath.to_s end |
#needs_resizing?(img, width) ⇒ Boolean
68 69 70 |
# File 'lib/jekyll-responsive-image/resize_handler.rb', line 68 def needs_resizing?(img, width) img.columns > width end |
#resize_image(img, config) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jekyll-responsive-image/resize_handler.rb', line 6 def resize_image(img, config) img.auto_orient! if config['auto_rotate'] 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, image_path, width, height) resized.push(image_hash(config, filepath, width, height)) site_source_filepath = File.(filepath, config[:site_source]) site_dest_filepath = File.(filepath, config[:site_dest]) if config['save_to_source'] target_filepath = site_source_filepath else target_filepath = site_dest_filepath end # 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 #{target_filepath}" if config['strip'] img.strip! end i = img.scale(ratio) i.write(target_filepath) do |f| f.interlace = i.interlace f.quality = size['quality'] || config['default_quality'] end 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 img.destroy! resized end |