class SrcsetImages::ImageVersion

Attributes

app[RW]
config[R]
img[R]
name[R]
resized_img_path[R]
width[R]

Public Class Methods

new(img, resized_img_path, config) click to toggle source

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

# 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

Public Instance Methods

base64_data() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 55
def base64_data
  Base64.strict_encode64 render
end
cached_resized_img_abs_path() click to toggle source

def middleman_abs_path

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

end

# 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?() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 47
def default?
  !!config[:is_default]
end
default_for_orientation?() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 51
def default_for_orientation?
  @default_for_orientation
end
img_path() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 43
def img_path
  img.rel_path
end
prepare_image() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 80
def prepare_image
  unless cached_image_available?
    save_cached_image
  end
end
render() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 59
def render
  prepare_image
  File.read cached_resized_img_abs_path
end

Private Instance Methods

build_dir() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 96
def build_dir
  app.config[:build_dir]
end
cached_image_available?() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 108
def cached_image_available?
  File.exist?(cached_resized_img_abs_path)
end
images_dir() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 92
def images_dir
  app.config[:images_dir]
end
save_cached_image() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 100
def save_cached_image
  FileUtils.mkdir_p(File.dirname(cached_resized_img_abs_path))
  VipsCreateImageVersion.(
    img.vips, cached_resized_img_abs_path,
    width: @width, height: @height, quality: @quality, crop: @crop
  )
end
source_dir() click to toggle source
# File lib/middleman-srcset_images/image_version.rb, line 88
def source_dir
  File.absolute_path(app.config[:source], @app.root)
end