class Thumbnailer::Base

Attributes

dir[R]
height[R]
image_height[R]
image_width[R]
object[RW]
prefix[R]
sequence[R]
width[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/thumbnailer/base.rb, line 11
def initialize(options = {})
  @options      = options
  @object       = options[:object]
  @ratio        = options[:ratio] || 2
  @width        = options[:width]
  @height       = options[:height]
  @image_width  = nil
  @image_height = nil
  @prefix       = options[:prefix] || 'thumb_'
  @dir          = options[:dir]
  @sequence     = nil

  extract_image_size
end

Public Instance Methods

create() click to toggle source
# File lib/thumbnailer/base.rb, line 33
def create
  write
end
output() click to toggle source
# File lib/thumbnailer/base.rb, line 26
def output
  path = "#{@dir}/#{@prefix}#{@file}"
  path = path.gsub(/\.(\w+)$/, ".#{@options[:format]}") if @options[:format].to_s.size > 0
  path = insert_page(path)
  path
end
write() click to toggle source
# File lib/thumbnailer/base.rb, line 37
def write
  resize
  @object.write(output)
end

Private Instance Methods

avail_thumb_size() click to toggle source
# File lib/thumbnailer/base.rb, line 62
def avail_thumb_size
  width, height = create_prop_size
  @width  = width  if @width.nil?
  @height = height if @height.nil?
end
create_prop_size() click to toggle source
# File lib/thumbnailer/base.rb, line 68
def create_prop_size
  width  = (@image_width  / @ratio).to_i if @image_width
  height = (@image_height / @ratio).to_i if @image_height
  [ width, height ]
end
extract_image_size() click to toggle source
# File lib/thumbnailer/base.rb, line 56
def extract_image_size
  return if !@object
  @image_width, @image_height = @object[:width], @object[:height]
  avail_thumb_size
end
insert_page(path) click to toggle source
# File lib/thumbnailer/base.rb, line 44
def insert_page(path)
  return path if @options[:sequence].to_i <= 0
  matcher = path.match(/(?<prefix>.*)(\.)(?<ext>\w+)$/)
  "#{matcher[:prefix]}_#{@options[:sequence]}.#{matcher[:ext]}"
end
resize() click to toggle source
# File lib/thumbnailer/base.rb, line 50
def resize
  return if @object.nil?
  @width, @heigth = avail_thumb_size
  @object.resize("#{@width}x#{@height}")
end