Class: Imogen::Iiif::Size

Inherits:
Transform show all
Defined in:
lib/imogen/iiif/size.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Transform

#initialize, #max, #min

Constructor Details

This class inherits a constructor from Imogen::Iiif::Transform

Class Method Details

.convert(img, size) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/imogen/iiif/size.rb', line 35

def self.convert(img, size)
  dims = Size.new(img).get(size)
  if dims
    yield img.thumbnail_image(dims[0], height: dims[1])
  else
    yield img
  end
end

Instance Method Details

#get(scale = nil) ⇒ Object

Raises:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/imogen/iiif/size.rb', line 4

def get(scale=nil)
  if scale.nil? or scale.eql? "full"
    return nil
  end
  if md = /^[!]?(\d+)?,(\d+)?$/.match(scale)
    w = md[1] ? min(Integer(md[1]), @width) : nil
    h = md[2] ? min(Integer(md[2]), @height) : nil
    raise BadRequest.new("bad scale #{scale}") unless w or h
    w ||= (@width * (h.to_f / @height)).round
    h ||= (@height * (w.to_f / @width)).round
    e = [w,h]
  elsif md = /^pct:(\d+(\.\d+)?)/.match(scale)
    p = Float(md[1])
    p = min(100,p).to_f
    raise BadRequest.new("bad size #{scale}") if p <= 0
    e = [(@width * p / 100).round, (@height * p / 100).round]
  else
    raise BadRequest.new("bad size #{scale}")
  end
  raise BadRequest.new("bad size #{scale}") if e[0] <= 0 or e[1] <= 0
  if scale.start_with? '!'
    w_ratio = e[0].to_f / @width
    h_ratio = e[1].to_f / @height
    if w_ratio > h_ratio
      e[0] = (@width * h_ratio).round
    elsif h_ratio > w_ratio
      e[1] = (@height * w_ratio).round
    end
  end
  return e
end