Class: SrcsetImages::ImageVersion

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(img, resized_img_path, config) ⇒ ImageVersion

resized_img_path is the wrong path here (posts/2013/08-17-kilimanjaro/bay_ls_0.jpg instead of 2013/08/kilimanjaro/bay_ls_0.jpg) but it does not seem to matter since this is apparently fixed by middleman itself through the VersionResource



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
# File 'lib/middleman-srcset_images/image_version.rb', line 15

def initialize(img, resized_img_path, config)
  @img = img
  @resized_img_path = resized_img_path
  @config = config

  @default_for_orientation = config[:name] == img.orientation

  @width   = config[:width]
  @height  = config[:height]

  if @width.nil? && @height.nil?
    raise ArgumentError, "need at least width or height!\nconfig was: #{config}"
  end

  ratio = config[:ratio] || img.xy_ratio
  if @width.blank?
    @width = (@height.to_f * ratio).to_i
  end
  if @height.blank?
    @height = (@width.to_f / ratio).to_i
  end

  @crop    = config.fetch :crop, false
  @quality = config.fetch :quality, 80
  @cache_dir = config[:cache_dir]
end

Instance Attribute Details

#app ⇒ Object

Returns the value of attribute app.



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

def app
  @app
end

#config ⇒ Object (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/middleman-srcset_images/image_version.rb', line 7

def config
  @config
end

#img ⇒ Object (readonly)

Returns the value of attribute img.



7
8
9
# File 'lib/middleman-srcset_images/image_version.rb', line 7

def img
  @img
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/middleman-srcset_images/image_version.rb', line 7

def name
  @name
end

#resized_img_path ⇒ Object (readonly)

Returns the value of attribute resized_img_path.



7
8
9
# File 'lib/middleman-srcset_images/image_version.rb', line 7

def resized_img_path
  @resized_img_path
end

#width ⇒ Object (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/middleman-srcset_images/image_version.rb', line 7

def width
  @width
end

Instance Method Details

#base64_data ⇒ Object



55
56
57
# File 'lib/middleman-srcset_images/image_version.rb', line 55

def base64_data
  Base64.strict_encode64 render
end

#cached_resized_img_abs_path ⇒ Object

def middleman_abs_path img_path.start_with?('/') ? img_path : File.join(images_dir, img_path) end



74
75
76
77
78
# File 'lib/middleman-srcset_images/image_version.rb', line 74

def cached_resized_img_abs_path
  File.join(@cache_dir, resized_img_path).split('.').tap { |a|
    a.insert(-2, img.checksum)
  }.join('.')
end

#default? ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/middleman-srcset_images/image_version.rb', line 47

def default?
  !!config[:is_default]
end

#default_for_orientation? ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/middleman-srcset_images/image_version.rb', line 51

def default_for_orientation?
  @default_for_orientation
end

#img_path ⇒ Object



43
44
45
# File 'lib/middleman-srcset_images/image_version.rb', line 43

def img_path
  img.rel_path
end

#prepare_image ⇒ Object



80
81
82
83
84
# File 'lib/middleman-srcset_images/image_version.rb', line 80

def prepare_image
  unless cached_image_available?
    save_cached_image
  end
end

#render ⇒ Object



59
60
61
62
# File 'lib/middleman-srcset_images/image_version.rb', line 59

def render
  prepare_image
  File.read cached_resized_img_abs_path
end