class Vips::Thumbnail::Resizer

Attributes

input_path[R]
options[R]

Public Class Methods

new(input_path = nil, **options, &block) click to toggle source
# 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

Public Instance Methods

close() click to toggle source
# File lib/vips/thumbnail/resizer.rb, line 38
def close
        @input_image&.close
        @input_image = nil
end
Also aliased as: flush!
flush!()
Alias for: close
input_aspect_ratio() click to toggle source
# File lib/vips/thumbnail/resizer.rb, line 53
def input_aspect_ratio
        Rational(input_image.width, input_image.height)
end
input_image() click to toggle source
# 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) click to toggle source

Resize the image to completely fill the desired size, if possible. @return [Vips::Image] if the image could be resized, otherwise `nil`.

# 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) click to toggle source

Resize the image to fit within the given bounds, preserving aspect ratio. @return [Vips::Image] if the image could be resized, otherwise `nil`.

# 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

Private Instance Methods

crop(image, width, height) click to toggle source
# File lib/vips/thumbnail/resizer.rb, line 130
def crop(image, width, height)
        left = (image.width - width) / 2.0
        top = (image.height - height) / 2.0
        
        # puts "image.width #{image.width} image.height: #{image.height}"
        # puts "Left: #{left} Top: #{top} X: #{width} Y: #{height}"
        image = image.extract_area(left, top, width, height)
        
        # puts "image.width #{image.width} image.height: #{image.height}"
        
        return image
end
fill_and_crop(image, width, height) click to toggle source
# File lib/vips/thumbnail/resizer.rb, line 102
def fill_and_crop(image, width, height)
        x_scale = Rational(width, image.width)
        y_scale = Rational(height, image.height)
        
        scale = [x_scale, y_scale].max
        # puts "scale #{scale}"
        
        if scale < 1.0
                image = image.resize(scale)
        elsif scale > 1.0
                # Just crop it.. no scale.
                width = image.width if x_scale > 1.0
                height = image.height if y_scale > 1.0
        end
        
        # If you hit these errors, it means you are using an older version of vips (< 8.4) which has an integer truncation bug.
        # https://github.com/jcupitt/ruby-vips/issues/82
        if image.height < height
                raise ArgumentError.new("Image resized smaller than crop! Scaled height #{image.height} < #{height}.")
        end
        
        if image.width < width
                raise ArgumentError.new("Image resized smaller than crop! Scaled width #{image.width} < #{width}.")
        end
        
        return crop(image, width, height)
end
fit(image, width, height) click to toggle source
# File lib/vips/thumbnail/resizer.rb, line 91
def fit(image, width, height)
        x_scale = Rational(width, image.width)
        y_scale = Rational(height, image.height)
        
        scale = [x_scale, y_scale].min
        
        if scale < 1.0
                return image.resize(scale)
        end
end
load() click to toggle source
# File lib/vips/thumbnail/resizer.rb, line 81
def load
        image = Vips::Image.new_from_file(@input_path, **@options)
         
        if image.respond_to?(:autorot)
                image = image.autorot
        end
        
        return image
end