class SrcsetImages::Img

Attributes

abs_path[R]
height[R]
path[R]
width[R]

Public Class Methods

new(path) click to toggle source
# File lib/middleman-srcset_images/img.rb, line 10
def initialize(path)
  @path = path
  @abs_path = Pathname(path).absolute? ? path : File.join(Dir.pwd, path)
end

Public Instance Methods

basename() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 41
def basename
  @basename ||= File.basename path, ext
end
checksum() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 53
def checksum
  @checksum ||= Digest::SHA2.file(abs_path).hexdigest[0..16]
end
ext() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 49
def ext
  @ext ||= File.extname path
end
filename() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 45
def filename
  File.basename path
end
landscape?() click to toggle source

true if landscape or square

# File lib/middleman-srcset_images/img.rb, line 24
def landscape?
  xy_ratio >= 1
end
orientation() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 15
def orientation
  landscape? ? 'landscape' : 'portrait'
end
path_for_version(cfg_name, idx) click to toggle source
# File lib/middleman-srcset_images/img.rb, line 37
def path_for_version(cfg_name, idx)
  "#{File.dirname rel_path}/#{basename}_#{cfg_name}_#{idx}#{ext}"
end
portrait?() click to toggle source

true if portrait

# File lib/middleman-srcset_images/img.rb, line 29
def portrait?
  xy_ratio < 1
end
rel_path() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 19
def rel_path
  @rel_path ||= Pathname(abs_path).relative_path_from(Pathname(Dir.pwd))
end
vips() click to toggle source
# File lib/middleman-srcset_images/img.rb, line 33
def vips
  @vips ||= ImageProcessing::Vips.source(abs_path)
end
xy_ratio() click to toggle source

TODO can get dimensions from vips?

# File lib/middleman-srcset_images/img.rb, line 58
def xy_ratio
  @xy_ratio ||= begin
    File.open(abs_path, 'rb') do |io|
      Dimensions(io)
      io.extend DimensionsPatch
      @width, @height = io.dimensions
      @width.to_f / @height
    end
  end
rescue
  nil
end