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, loader: nil, autorot: true, **options) ⇒ Object
Loads the image on disk into a Vips::Image object.
-
.save_image(image, path, saver: nil, quality: nil, **options) ⇒ Object
Writes the Vips::Image object to disk.
-
.supports_resize_on_load? ⇒ Boolean
See #thumbnail.
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_cover(width, height, **options) ⇒ Object
Resizes the image to cover the specified dimensions, without cropping the excess.
-
#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 metadata setter methods chainable.
- #set_type(*args) ⇒ Object
- #set_value(*args) ⇒ Object
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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/image_processing/vips.rb', line 34 def self.load_image(path_or_image, loader: nil, autorot: true, **) 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, **) else = Utils.(path, ) image = ::Vips::Image.new_from_file(path, **) end end image = image.autorot if autorot && !.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).
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/image_processing/vips.rb', line 60 def self.save_image(image, path, saver: nil, quality: nil, **) [:Q] = quality if quality if saver image.public_send(:"#{saver}save", path, **) else = Utils.(path, ) image.write_to_file(path, **) end end |
.supports_resize_on_load? ⇒ Boolean
See #thumbnail.
53 54 55 |
# File 'lib/image_processing/vips.rb', line 53 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/image_processing/vips.rb', line 126 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
156 |
# File 'lib/image_processing/vips.rb', line 156 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.
91 92 93 94 95 |
# File 'lib/image_processing/vips.rb', line 91 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_cover(width, height, **options) ⇒ Object
Resizes the image to cover the specified dimensions, without cropping the excess.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/image_processing/vips.rb', line 99 def resize_to_cover(width, height, **) image = self.image.is_a?(String) ? self.class.load_image(self.image) : self.image image_ratio = Rational(image.width, image.height) thumbnail_ratio = Rational(width, height) if image_ratio > thumbnail_ratio width = ::Vips::MAX_COORD else height = ::Vips::MAX_COORD end thumbnail(width, height, **, crop: :none) end |
#resize_to_fill(width, height, **options) ⇒ Object
Resizes the image to fill the specified dimensions, applying any necessary cropping.
85 86 87 |
# File 'lib/image_processing/vips.rb', line 85 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.
78 79 80 81 |
# File 'lib/image_processing/vips.rb', line 78 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.
72 73 74 75 |
# File 'lib/image_processing/vips.rb', line 72 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.
115 116 117 118 119 120 121 122 |
# File 'lib/image_processing/vips.rb', line 115 def rotate(degrees, **) if ([90, 180, 270].include?(degrees) && .empty?) rot_command = "rot#{degrees}".to_sym image.public_send rot_command else image.similarity(angle: degrees, **) end end |
#set(*args) ⇒ Object
make metadata setter methods chainable
153 |
# File 'lib/image_processing/vips.rb', line 153 def set(*args) image.tap { |img| img.set(*args) } end |
#set_type(*args) ⇒ Object
154 |
# File 'lib/image_processing/vips.rb', line 154 def set_type(*args) image.tap { |img| img.set_type(*args) } end |
#set_value(*args) ⇒ Object
155 |
# File 'lib/image_processing/vips.rb', line 155 def set_value(*args) image.tap { |img| img.set_value(*args) } end |