Class: ImageProcessing::Vips::Processor
- 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
-
.load_image(path_or_image, autorot: true, **options) ⇒ Object
Loads the image on disk into a Vips::Image object.
-
.save_image(image, destination_path, quality: nil, **options) ⇒ Object
Writes the Vips::Image object to disk.
Instance Method Summary collapse
-
#composite(overlay, _mode = nil, mode: "over", gravity: "north-west", offset: nil, **options) ⇒ Object
Overlays the specified image over the current one.
- #remove(*args) ⇒ Object
-
#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.
-
#resize_to_fill(width, height, **options) ⇒ Object
Resizes the image to fill the specified dimensions, applying any necessary cropping.
-
#resize_to_fit(width, height, **options) ⇒ Object
Resizes the image to fit within the specified dimensions.
-
#resize_to_limit(width, height, **options) ⇒ Object
Resizes the image to not be larger than the specified dimensions.
-
#rotate(degrees, **options) ⇒ Object
Rotates the image by an arbitrary angle.
-
#set(*args) ⇒ Object
make Vips::Image#set, #set_type, #set_value, and #move chainable.
- #set_type(*args) ⇒ Object
- #set_value(*args) ⇒ Object
Methods inherited from Processor
accumulator, apply_operation, #custom, #initialize
Constructor Details
This class inherits a constructor from ImageProcessing::Processor
Class Method Details
.load_image(path_or_image, 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 |
# File 'lib/image_processing/vips.rb', line 30 def self.load_image(path_or_image, autorot: true, **) if path_or_image.is_a?(::Vips::Image) image = path_or_image else source_path = path_or_image = Utils.(source_path, ) image = ::Vips::Image.new_from_file(source_path, **) end image = image.autorot if autorot && !.key?(:autorotate) image end |
.save_image(image, destination_path, 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).
47 48 49 50 51 52 |
# File 'lib/image_processing/vips.rb', line 47 def self.save_image(image, destination_path, quality: nil, **) = .merge(Q: quality) if quality = Utils.(destination_path, ) image.write_to_file(destination_path, **) 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/image_processing/vips.rb', line 89 def composite(, _mode = nil, mode: "over", gravity: "north-west", offset: nil, **) # if the mode argument is given, call the original Vips::Image#composite if _mode = [] unless .is_a?(Array) = .map { |object| convert_to_image(object, "overlay") } return image.composite(, _mode, **) end = convert_to_image(, "overlay") # add alpha channel so that #gravity can use a transparent background = .add_alpha unless .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") = .gravity(opposite_gravity, .width + offset.first, .height + offset.last) end # create image-sized transparent background and apply specified gravity = .gravity(gravity, image.width, image.height) # apply the composition image.composite(, mode, **) end |
#remove(*args) ⇒ Object
119 |
# File 'lib/image_processing/vips.rb', line 119 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.
74 75 76 77 78 |
# File 'lib/image_processing/vips.rb', line 74 def resize_and_pad(width, height, gravity: "centre", extend: nil, background: nil, alpha: nil, **) image = thumbnail(width, height, **) 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.
68 69 70 |
# File 'lib/image_processing/vips.rb', line 68 def resize_to_fill(width, height, **) thumbnail(width, height, crop: :centre, **) end |
#resize_to_fit(width, height, **options) ⇒ Object
Resizes the image to fit within the specified dimensions.
61 62 63 64 |
# File 'lib/image_processing/vips.rb', line 61 def resize_to_fit(width, height, **) width, height = default_dimensions(width, height) thumbnail(width, height, **) end |
#resize_to_limit(width, height, **options) ⇒ Object
Resizes the image to not be larger than the specified dimensions.
55 56 57 58 |
# File 'lib/image_processing/vips.rb', line 55 def resize_to_limit(width, height, **) width, height = default_dimensions(width, height) thumbnail(width, height, size: :down, **) end |
#rotate(degrees, **options) ⇒ Object
Rotates the image by an arbitrary angle. Additional options can be specified, such as background colors to fill in the gaps when rotating with an angle which is not a multiple of 90 degrees.
83 84 85 |
# File 'lib/image_processing/vips.rb', line 83 def rotate(degrees, **) image.similarity(angle: degrees, **) end |
#set(*args) ⇒ Object
make Vips::Image#set, #set_type, #set_value, and #move chainable
116 |
# File 'lib/image_processing/vips.rb', line 116 def set(*args) image.tap { |img| img.set(*args) } end |
#set_type(*args) ⇒ Object
117 |
# File 'lib/image_processing/vips.rb', line 117 def set_type(*args) image.tap { |img| img.set_type(*args) } end |
#set_value(*args) ⇒ Object
118 |
# File 'lib/image_processing/vips.rb', line 118 def set_value(*args) image.tap { |img| img.set_value(*args) } end |