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
- .save_image(image, destination_path, quality: nil, **options) ⇒ Object
Instance Method Summary collapse
- #composite(overlay, _mode = nil, mode: :over, gravity: :"north-west", offset: nil, **options) ⇒ Object
- #resize_and_pad(width, height, gravity: "centre", extend: nil, background: nil, alpha: nil, **options) ⇒ Object
- #resize_to_fill(width, height, **options) ⇒ Object
- #resize_to_fit(width, height, **options) ⇒ Object
- #resize_to_limit(width, height, **options) ⇒ Object
- #rotate(degrees, background: nil) ⇒ Object
-
#set(*args) ⇒ Object
make Vips::Image#set, #set_type, and #set_value 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
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/image_processing/vips.rb', line 26 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
40 41 42 43 44 45 |
# File 'lib/image_processing/vips.rb', line 40 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/image_processing/vips.rb', line 81 def composite(, _mode = nil, mode: :over, gravity: :"north-west", offset: nil, **) if _mode = [] unless .is_a?(Array) = .map { |object| convert_to_image(object, "overlay") } return image.composite(, _mode, **) end = convert_to_image(, "overlay") = .add_alpha unless .has_alpha? # so that #gravity can use transparent background 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 = .gravity(gravity, image.width, image.height) image.composite(, mode, **) end |
#resize_and_pad(width, height, gravity: "centre", extend: nil, background: nil, alpha: nil, **options) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/image_processing/vips.rb', line 61 def resize_and_pad(width, height, gravity: "centre", extend: nil, background: nil, alpha: nil, **) = { extend: extend, background: background } .reject! { |name, value| value.nil? } image = thumbnail(width, height, **) image = image.add_alpha if alpha && !image.has_alpha? image.gravity(gravity, width, height, **) end |
#resize_to_fill(width, height, **options) ⇒ Object
57 58 59 |
# File 'lib/image_processing/vips.rb', line 57 def resize_to_fill(width, height, **) thumbnail(width, height, crop: :centre, **) end |
#resize_to_fit(width, height, **options) ⇒ Object
52 53 54 55 |
# File 'lib/image_processing/vips.rb', line 52 def resize_to_fit(width, height, **) width, height = default_dimensions(width, height) thumbnail(width, height, **) end |
#resize_to_limit(width, height, **options) ⇒ Object
47 48 49 50 |
# File 'lib/image_processing/vips.rb', line 47 def resize_to_limit(width, height, **) width, height = default_dimensions(width, height) thumbnail(width, height, size: :down, **) end |
#rotate(degrees, background: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/image_processing/vips.rb', line 70 def rotate(degrees, background: nil) if degrees % 90 == 0 image.rot(:"d#{degrees % 360}") else = { angle: degrees } [:background] = background if background image.similarity(**) end end |
#set(*args) ⇒ Object
make Vips::Image#set, #set_type, and #set_value chainable
103 |
# File 'lib/image_processing/vips.rb', line 103 def set(*args) image.tap { |img| img.set(*args) } end |
#set_type(*args) ⇒ Object
104 |
# File 'lib/image_processing/vips.rb', line 104 def set_type(*args) image.tap { |img| img.set_type(*args) } end |
#set_value(*args) ⇒ Object
105 |
# File 'lib/image_processing/vips.rb', line 105 def set_value(*args) image.tap { |img| img.set_value(*args) } end |