Class: MountableImageServer::ImageProcessor

Inherits:
Object
  • Object
show all
Includes:
Skeptick
Defined in:
lib/mountable_image_server/image_processor.rb

Constant Summary collapse

VALID_PARAMETERS =
[:fit, :crop, :w, :h, :q, :darken]

Instance Method Summary collapse

Constructor Details

#initialize(path, parameters) ⇒ ImageProcessor

Returns a new instance of ImageProcessor.



11
12
13
14
15
16
17
18
# File 'lib/mountable_image_server/image_processor.rb', line 11

def initialize(path, parameters)
  @path = path
  @parameters = parameters.select do |key, value|
    VALID_PARAMETERS.include?(key.to_sym)
  end.map do |key, value|
    [key.to_sym, value]
  end.to_h
end

Instance Method Details

#run(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mountable_image_server/image_processor.rb', line 20

def run(&block)
  yield(Pathname(path)) and return unless parameters.any?

  parameters[:fit] ||= 'clip'

  operations_queue = [
    resize_operations,
    crop_operations,
    quality_operations,
    darken_operations
  ].reduce([], :+)

  Tempfile.create(['processed-image', path.extname]) do |file|
    command = convert(path, to: file.path) do
      operations_queue.each do |operation|
        set *operation
      end
    end

    command.run

    yield(Pathname(file.path))
  end
end