Class: Riiif::VipsResize

Inherits:
Object
  • Object
show all
Defined in:
app/services/riiif/vips_resize.rb

Overview

Represents a resize operation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, image) ⇒ VipsResize

Returns a new instance of VipsResize.



4
5
6
7
# File 'app/services/riiif/vips_resize.rb', line 4

def initialize(size, image)
  @size = size
  @image = image
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



9
10
11
# File 'app/services/riiif/vips_resize.rb', line 9

def image
  @image
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'app/services/riiif/vips_resize.rb', line 9

def size
  @size
end

Instance Method Details

#resize_ratio(side, image) ⇒ Float

Returns - the scale or percentage to resize the image by; passed to Vips::Image#resize.

Parameters:

  • - (Symbol)

    which side of the image to calculate, either :width or :height

Returns:

  • (Float)
    • the scale or percentage to resize the image by; passed to Vips::Image#resize



37
38
39
40
41
42
43
44
45
# File 'app/services/riiif/vips_resize.rb', line 37

def resize_ratio(side, image)
  length = image.send(side)
  target_length = size.send(side)
  if target_length < length
    target_length / length.to_f # Size down
  else
    length / target_length.to_f # Size up
  end
end

#to_vipsObject

Returns the parameters that vips will use to resize the image. This can be

  1. A [Float] representing the scale factor, passed to Vips::Image#resize

  2. An [Array], where the 1st elem is an Integer and the 2nd is a

    Hash of options, passed to Vips::Image#thumbnail
    
  3. NilClass

    when image should not be resized at all.

Returns:

  • the parameters that vips will use to resize the image. This can be

    1. A [Float] representing the scale factor, passed to Vips::Image#resize

    2. An [Array], where the 1st elem is an Integer and the 2nd is a

      Hash of options, passed to Vips::Image#thumbnail
      
    3. NilClass

      when image should not be resized at all



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/riiif/vips_resize.rb', line 16

def to_vips
  case size
  when IIIF::Image::Size::Percent
    size.percentage
  when IIIF::Image::Size::Width
    resize_ratio(:width, image)
  when IIIF::Image::Size::Height
    resize_ratio(:height, image)
  when IIIF::Image::Size::Absolute
    [size.width, { height: size.height, size: :force }]
  when IIIF::Image::Size::BestFit
    [size.width, { height: size.height }]
  when IIIF::Image::Size::Max, IIIF::Image::Size::Full
    nil
  else
    raise "unknown size #{size.class}"
  end
end