class Jekyll::SrcsetTag::Image::Instance

Attributes

extension[R]
fallback[R]
height[R]
image[R]
image_height[R]
image_width[R]
output_height[R]
output_width[R]
undersized[R]
width[R]

Public Class Methods

new(width:, height:, extension:, image:, fallback:) click to toggle source
# File lib/jekyll/srcset_tag/image/instance.rb, line 8
def initialize(width:, height:, extension:, image:, fallback:)
  @width = width
  @height = height
  @extension = extension
  @image = image
  @image_width = image[:width].to_i
  @image_height = image[:height].to_i
  @fallback = fallback
  calculate_output_dimensions!
end

Public Instance Methods

filename() click to toggle source
# File lib/jekyll/srcset_tag/image/instance.rb, line 19
def filename
  output_width.to_s + 'x' + output_height.to_s + extension
end
undersized?() click to toggle source
# File lib/jekyll/srcset_tag/image/instance.rb, line 23
def undersized?
  @undersized
end

Protected Instance Methods

calculate_output_dimensions!() click to toggle source

This is pretty much taken verbatim from github.com/robwierzbowski/jekyll-picture-tag

# File lib/jekyll/srcset_tag/image/instance.rb, line 30
def calculate_output_dimensions!
  @undersized = false
  image_ratio = image_width.to_f / image_height.to_f

  generated_width = if width
                      width.to_f
                    elsif height
                      image_ratio * height.to_f
                    else
                      image_width.to_f
                    end
  generated_height = if height
                       height.to_f
                     elsif width
                       width.to_f / image_ratio
                     else
                       image_height.to_f
                     end
  generated_ratio = generated_width / generated_height

  if image_width < generated_width || image_height < generated_height
    @undersized = true
    generated_width = if image_ratio < generated_ratio
                        image_width
                      else
                        image_height * generated_ratio
                      end
    generated_height = if image_ratio > generated_ratio
                         image_height
                       else
                         image_width / generated_ratio
                       end
  end

  @output_width = generated_width.round
  @output_height = generated_height.round
end