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, #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, **options)
  if path_or_image.is_a?(::Vips::Image)
    image = path_or_image
  else
    source_path = path_or_image
    options     = Utils.select_valid_loader_options(source_path, options)

    image = ::Vips::Image.new_from_file(source_path, **options)
  end

  image = image.autorot if autorot && !options.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, **options)
  options = options.merge(Q: quality) if quality
  options = Utils.select_valid_saver_options(destination_path, options)

  image.write_to_file(destination_path, **options)
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(overlay, _mode = nil, mode: :over, gravity: :"north-west", offset: nil, **options)
  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")
  overlay = overlay.add_alpha unless overlay.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")
    overlay = overlay.gravity(opposite_gravity, overlay.width + offset.first, overlay.height + offset.last)
  end

  overlay = overlay.gravity(gravity, image.width, image.height)

  image.composite(overlay, mode, **options)
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, **options)
  embed_options = { extend: extend, background: background }
  embed_options.reject! { |name, value| value.nil? }

  image = thumbnail(width, height, **options)
  image = image.add_alpha if alpha && !image.has_alpha?
  image.gravity(gravity, width, height, **embed_options)
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, **options)
  thumbnail(width, height, crop: :centre, **options)
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, **options)
  width, height = default_dimensions(width, height)
  thumbnail(width, height, **options)
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, **options)
  width, height = default_dimensions(width, height)
  thumbnail(width, height, size: :down, **options)
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
    options = { angle: degrees }
    options[:background] = background if background

    image.similarity(**options)
  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