Class: ImageProcessing::Vips::Processor

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

Constant Summary collapse

IMAGE_CLASS =
::Vips::Image
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

Instance Method Summary collapse

Methods inherited from Processor

#custom, #initialize

Constructor Details

This class inherits a constructor from ImageProcessing::Processor

Instance Method Details

#apply_operation(name, image, *args) ⇒ Object



22
23
24
25
# File 'lib/image_processing/vips.rb', line 22

def apply_operation(name, image, *args)
  result = super
  result.is_a?(::Vips::Image) ? result : image
end

#load_image(path_or_image, autorot: true, **options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/image_processing/vips.rb', line 50

def 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     = 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

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



41
42
43
44
45
46
47
48
# File 'lib/image_processing/vips.rb', line 41

def resize_and_pad(image, 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 = generate_thumbnail(image, width, height, **options)
  image = add_alpha(image) if alpha && !has_alpha?(image)
  image.gravity(gravity, width, height, **embed_options)
end

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



37
38
39
# File 'lib/image_processing/vips.rb', line 37

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

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



32
33
34
35
# File 'lib/image_processing/vips.rb', line 32

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

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



27
28
29
30
# File 'lib/image_processing/vips.rb', line 27

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

#save_image(image, destination_path, quality: nil, **options) ⇒ Object



64
65
66
67
68
69
# File 'lib/image_processing/vips.rb', line 64

def save_image(image, destination_path, quality: nil, **options)
  options = options.merge(Q: quality) if quality
  options = select_valid_saver_options(destination_path, options)

  image.write_to_file(destination_path, **options)
end