Class: Vips::Thumbnail::Resizer

Inherits:
Object
  • Object
show all
Defined in:
lib/vips/thumbnail/resizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path = nil, **options, &block) ⇒ Resizer

Returns a new instance of Resizer.



26
27
28
29
30
31
32
33
# File 'lib/vips/thumbnail/resizer.rb', line 26

def initialize(input_path = nil, **options, &block)
	@input_path = input_path
	@options = options
	
	@block = block || self.method(:load)
	
	@input_image = nil
end

Instance Attribute Details

#input_pathObject (readonly)

Returns the value of attribute input_path.



35
36
37
# File 'lib/vips/thumbnail/resizer.rb', line 35

def input_path
  @input_path
end

#optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/vips/thumbnail/resizer.rb', line 36

def options
  @options
end

Instance Method Details

#closeObject Also known as: flush!



38
39
40
41
# File 'lib/vips/thumbnail/resizer.rb', line 38

def close
	@input_image&.close
	@input_image = nil
end

#input_aspect_ratioObject



53
54
55
# File 'lib/vips/thumbnail/resizer.rb', line 53

def input_aspect_ratio
	Rational(input_image.width, input_image.height)
end

#input_imageObject



45
46
47
48
49
50
51
# File 'lib/vips/thumbnail/resizer.rb', line 45

def input_image
	unless @input_image
		@input_image = @block.call
	end
	
	return @input_image
end

#resize_to_fill(output_size) ⇒ Vips::Image

Resize the image to completely fill the desired size, if possible.

Returns:

  • (Vips::Image)

    if the image could be resized, otherwise nil.



59
60
61
62
63
64
65
66
# File 'lib/vips/thumbnail/resizer.rb', line 59

def resize_to_fill(output_size)
	width, height = *output_size
	
	# We can only fill and crop if the input image is BIGGER in at least one dimension than the desired output size:
	if input_image.width > width or input_image.height > height
		return fill_and_crop(input_image, width, height)
	end
end

#resize_to_fit(output_size) ⇒ Vips::Image

Resize the image to fit within the given bounds, preserving aspect ratio.

Returns:

  • (Vips::Image)

    if the image could be resized, otherwise nil.



70
71
72
73
74
75
76
77
# File 'lib/vips/thumbnail/resizer.rb', line 70

def resize_to_fit(output_size)
	width, height = *output_size
	
	# We can only fit if the input image is BIGGER in at least one dimension than the desired output size:
	if input_image.width > width or input_image.height > height
		return fit(input_image, width, height)
	end
end