Class: ImageProcessing::Vips::Processor

Inherits:
Processor
  • Object
show all
Defined in:
lib/image_processing/vips.rb

Defined Under Namespace

Modules: Utils

Constant Summary collapse

SHARPEN_MASK =

Default sharpening mask that provides a fast and mild sharpen.

::Vips::Image.new_from_array [[-1, -1, -1],
[-1, 32, -1],
[-1, -1, -1]], 24

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

accumulator, apply_operation, #apply_operation, call, #custom, #initialize

Constructor Details

This class inherits a constructor from ImageProcessing::Processor

Class Method Details

.load_image(path_or_image, loader: nil, autorot: true, **options) ⇒ Object

Loads the image on disk into a Vips::Image object. Accepts additional loader-specific options (e.g. interlacing). Afterwards auto-rotates the image to be upright.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/image_processing/vips.rb', line 30

def self.load_image(path_or_image, loader: nil, autorot: true, **options)
  if path_or_image.is_a?(::Vips::Image)
    image = path_or_image
  else
    path = path_or_image

    if loader
      image = ::Vips::Image.public_send(:"#{loader}load", path, **options)
    else
      options = Utils.select_valid_loader_options(path, options)
      image = ::Vips::Image.new_from_file(path, **options)
    end
  end

  image = image.autorot if autorot && !options.key?(:autorotate)
  image
end

.save_image(image, path, saver: nil, quality: nil, **options) ⇒ Object

Writes the Vips::Image object to disk. This starts the processing pipeline defined in the Vips::Image object. Accepts additional saver-specific options (e.g. quality).



56
57
58
59
60
61
62
63
64
65
# File 'lib/image_processing/vips.rb', line 56

def self.save_image(image, path, saver: nil, quality: nil, **options)
  options[:Q] = quality if quality

  if saver
    image.public_send(:"#{saver}save", path, **options)
  else
    options = Utils.select_valid_saver_options(path, options)
    image.write_to_file(path, **options)
  end
end

.supports_resize_on_load?Boolean

See #thumbnail.

Returns:

  • (Boolean)


49
50
51
# File 'lib/image_processing/vips.rb', line 49

def self.supports_resize_on_load?
  true
end

Instance Method Details

#composite(overlay, _mode = nil, mode: "over", gravity: "north-west", offset: nil, **options) ⇒ Object

Overlays the specified image over the current one. Supports specifying composite mode, direction or offset of the overlay image.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/image_processing/vips.rb', line 100

def composite(overlay, _mode = nil, mode: "over", gravity: "north-west", offset: nil, **options)
  # if the mode argument is given, call the original Vips::Image#composite
  if _mode
    overlay = [overlay] unless overlay.is_a?(Array)
    overlay = overlay.map { |object| convert_to_image(object, "overlay") }

    return image.composite(overlay, _mode, **options)
  end

  overlay = convert_to_image(overlay, "overlay")
  # add alpha channel so that #gravity can use a transparent background
  overlay = overlay.add_alpha unless overlay.has_alpha?

  # apply offset with correct gravity and make remainder transparent
  if offset
    opposite_gravity = gravity.to_s.gsub(/\w+/, "north"=>"south", "south"=>"north", "east"=>"west", "west"=>"east")
    overlay = overlay.gravity(opposite_gravity, overlay.width + offset.first, overlay.height + offset.last)
  end

  # create image-sized transparent background and apply specified gravity
  overlay = overlay.gravity(gravity, image.width, image.height)

  # apply the composition
  image.composite(overlay, mode, **options)
end

#remove(*args) ⇒ Object



130
# File 'lib/image_processing/vips.rb', line 130

def remove(*args)    image.tap { |img| img.remove(*args) }    end

#resize_and_pad(width, height, gravity: "centre", extend: nil, background: nil, alpha: nil, **options) ⇒ Object

Resizes the image to fit within the specified dimensions and fills the remaining area with the specified background color.



87
88
89
90
91
# File 'lib/image_processing/vips.rb', line 87

def resize_and_pad(width, height, gravity: "centre", extend: nil, background: nil, alpha: nil, **options)
  image = thumbnail(width, height, **options)
  image = image.add_alpha if alpha && !image.has_alpha?
  image.gravity(gravity, width, height, extend: extend, background: background)
end

#resize_to_fill(width, height, **options) ⇒ Object

Resizes the image to fill the specified dimensions, applying any necessary cropping.



81
82
83
# File 'lib/image_processing/vips.rb', line 81

def resize_to_fill(width, height, **options)
  thumbnail(width, height, crop: :centre, **options)
end

#resize_to_fit(width, height, **options) ⇒ Object

Resizes the image to fit within the specified dimensions.



74
75
76
77
# File 'lib/image_processing/vips.rb', line 74

def resize_to_fit(width, height, **options)
  width, height = default_dimensions(width, height)
  thumbnail(width, height, **options)
end

#resize_to_limit(width, height, **options) ⇒ Object

Resizes the image to not be larger than the specified dimensions.



68
69
70
71
# File 'lib/image_processing/vips.rb', line 68

def resize_to_limit(width, height, **options)
  width, height = default_dimensions(width, height)
  thumbnail(width, height, size: :down, **options)
end

#rotate(degrees, **options) ⇒ Object

Rotates the image by an arbitrary angle.



94
95
96
# File 'lib/image_processing/vips.rb', line 94

def rotate(degrees, **options)
  image.similarity(angle: degrees, **options)
end

#set(*args) ⇒ Object

make metadata setter methods chainable



127
# File 'lib/image_processing/vips.rb', line 127

def set(*args)       image.tap { |img| img.set(*args) }       end

#set_type(*args) ⇒ Object



128
# File 'lib/image_processing/vips.rb', line 128

def set_type(*args)  image.tap { |img| img.set_type(*args) }  end

#set_value(*args) ⇒ Object



129
# File 'lib/image_processing/vips.rb', line 129

def set_value(*args) image.tap { |img| img.set_value(*args) } end