Class: ImageProcessing::Vips::Processor

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

Constant Summary collapse

IMAGE_CLASS =
::Vips::Image
MAX_COORD =

libvips has this arbitrary number as a sanity-check upper bound on image size.

10_000_000

Instance Method Summary collapse

Instance Method Details

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



23
24
25
26
27
28
29
30
# File 'lib/image_processing/vips.rb', line 23

def apply_operation(name, image, *args)
  if respond_to?(name)
    public_send(name, image, *args)
  else
    result = image.send(name, *args)
    result.is_a?(::Vips::Image) ? result : image
  end
end

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/image_processing/vips.rb', line 55

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
    loader  = ::Vips.vips_foreign_find_load(source_path)
    options = select_valid_options(loader, options) if loader

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

  image = image.autorot if autorot
  image
end

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



46
47
48
49
50
51
52
53
# File 'lib/image_processing/vips.rb', line 46

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 = image.thumbnail_image(width, height: height, **options)
  image = image.bandjoin(255) if alpha && image.bands == 3
  image.gravity(gravity, width, height, **embed_options)
end

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



42
43
44
# File 'lib/image_processing/vips.rb', line 42

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

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



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

def resize_to_fit(image, width, height, **options)
  width, height = default_dimensions(width, height)
  image.thumbnail_image(width, height: height, **options)
end

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



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

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

#save_image(image, destination_path, **options) ⇒ Object



70
71
72
73
74
75
# File 'lib/image_processing/vips.rb', line 70

def save_image(image, destination_path, **options)
  saver   = ::Vips.vips_foreign_find_save(destination_path)
  options = select_valid_options(saver, options) if saver

  image.write_to_file(destination_path, **options)
end