Class: SrcsetImages::Img
- Inherits:
-
Object
- Object
- SrcsetImages::Img
- Defined in:
- lib/middleman-srcset_images/img.rb
Instance Attribute Summary collapse
-
#abs_path ⇒ Object
readonly
Returns the value of attribute abs_path.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #basename ⇒ Object
- #checksum ⇒ Object
- #ext ⇒ Object
- #filename ⇒ Object
-
#initialize(path) ⇒ Img
constructor
A new instance of Img.
-
#landscape? ⇒ Boolean
true if landscape or square.
- #orientation ⇒ Object
- #path_for_version(cfg_name, idx) ⇒ Object
-
#portrait? ⇒ Boolean
true if portrait.
- #rel_path ⇒ Object
- #vips ⇒ Object
-
#xy_ratio ⇒ Object
TODO can get dimensions from vips?.
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_path ⇒ Object (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 |
#height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/middleman-srcset_images/img.rb', line 8 def height @height end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/middleman-srcset_images/img.rb', line 8 def path @path end |
#width ⇒ Object (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
#basename ⇒ Object
41 42 43 |
# File 'lib/middleman-srcset_images/img.rb', line 41 def basename @basename ||= File.basename path, ext end |
#checksum ⇒ Object
53 54 55 |
# File 'lib/middleman-srcset_images/img.rb', line 53 def checksum @checksum ||= Digest::SHA2.file(abs_path).hexdigest[0..16] end |
#ext ⇒ Object
49 50 51 |
# File 'lib/middleman-srcset_images/img.rb', line 49 def ext @ext ||= File.extname path end |
#filename ⇒ Object
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
24 25 26 |
# File 'lib/middleman-srcset_images/img.rb', line 24 def landscape? xy_ratio >= 1 end |
#orientation ⇒ Object
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
29 30 31 |
# File 'lib/middleman-srcset_images/img.rb', line 29 def portrait? xy_ratio < 1 end |
#rel_path ⇒ Object
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 |
#vips ⇒ Object
33 34 35 |
# File 'lib/middleman-srcset_images/img.rb', line 33 def vips @vips ||= ImageProcessing::Vips.source(abs_path) end |
#xy_ratio ⇒ Object
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 |