Class: ImageProcessing::Vips::Processor

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

Constant Summary collapse

MAX_COORD =

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

10_000_000

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Processor

Returns a new instance of Processor.



15
16
17
18
19
20
# File 'lib/image_processing/vips/processor.rb', line 15

def initialize(source)
  fail Error, "source file not provided" unless source
  fail Error, "source file doesn't respond to #path" unless source.respond_to?(:path) || source.is_a?(::Vips::Image)

  @source = source
end

Instance Method Details

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



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

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(**options) ⇒ Object



50
51
52
53
54
# File 'lib/image_processing/vips/processor.rb', line 50

def load_image(**options)
  return @source if @source.is_a?(::Vips::Image)

  ::Vips::Image.new_from_file(@source.path, fail: true, **options)
end

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



45
46
47
48
# File 'lib/image_processing/vips/processor.rb', line 45

def resize_and_pad(image, width, height, background: "opaque", gravity: "centre", **options)
  image.thumbnail_image(width, height: height, **options)
    .gravity(gravity, width, height, extend: :background, background: Color.get(background))
end

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



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

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



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

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



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

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, format, **options) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/image_processing/vips/processor.rb', line 56

def save_image(image, format, **options)
  format ||= default_format
  result = Tempfile.new(["image_processing-vips", ".#{format}"], binmode: true)

  image.write_to_file(result.path, **options)
  result.open # refresh content

  result
end