Class: SrcsetImages::Img

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-srcset_images/img.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Img

Returns a new instance of Img.



10
11
12
13
# 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

Instance Attribute Details

#abs_pathObject (readonly)

Returns the value of attribute abs_path.



8
9
10
# File 'lib/middleman-srcset_images/img.rb', line 8

def abs_path
  @abs_path
end

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/middleman-srcset_images/img.rb', line 8

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/middleman-srcset_images/img.rb', line 8

def path
  @path
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/middleman-srcset_images/img.rb', line 8

def width
  @width
end

Instance Method Details

#basenameObject



41
42
43
# File 'lib/middleman-srcset_images/img.rb', line 41

def basename
  @basename ||= File.basename path, ext
end

#checksumObject



53
54
55
# File 'lib/middleman-srcset_images/img.rb', line 53

def checksum
  @checksum ||= Digest::SHA2.file(abs_path).hexdigest[0..16]
end

#extObject



49
50
51
# File 'lib/middleman-srcset_images/img.rb', line 49

def ext
  @ext ||= File.extname path
end

#filenameObject



45
46
47
# File 'lib/middleman-srcset_images/img.rb', line 45

def filename
  File.basename path
end

#landscape?Boolean

true if landscape or square

Returns:

  • (Boolean)


24
25
26
# File 'lib/middleman-srcset_images/img.rb', line 24

def landscape?
  xy_ratio >= 1
end

#orientationObject



15
16
17
# File 'lib/middleman-srcset_images/img.rb', line 15

def orientation
  landscape? ? 'landscape' : 'portrait'
end

#path_for_version(cfg_name, idx) ⇒ Object



37
38
39
# 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?Boolean

true if portrait

Returns:

  • (Boolean)


29
30
31
# File 'lib/middleman-srcset_images/img.rb', line 29

def portrait?
  xy_ratio < 1
end

#rel_pathObject



19
20
21
# File 'lib/middleman-srcset_images/img.rb', line 19

def rel_path
  @rel_path ||= Pathname(abs_path).relative_path_from(Pathname(Dir.pwd))
end

#vipsObject



33
34
35
# File 'lib/middleman-srcset_images/img.rb', line 33

def vips
  @vips ||= ImageProcessing::Vips.source(abs_path)
end

#xy_ratioObject

TODO can get dimensions from vips?



58
59
60
61
62
63
64
65
66
67
68
69
# 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