class BPL::Derivatives::Processors::Image

Public Instance Methods

process() click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 7
def process
  timeout ? process_with_timeout : create_resized_image
end
process_with_timeout() click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 11
def process_with_timeout
  Timeout.timeout(timeout) { create_resized_image }
rescue Timeout::Error
  raise BPL::Derivatives::TimeoutError, "Unable to process image derivative\nThe command took longer than #{timeout} seconds to execute"
end

Protected Instance Methods

create_image() { |xfrm| ... } click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 30
def create_image
  xfrm = selected_layers(load_image_transformer)
  yield(xfrm) if block_given?
  xfrm.format(directives.fetch(:format))
  xfrm.quality(quality.to_s) if quality
  xfrm.density(density.to_s) if density
  write_image(xfrm)
end
create_resized_image() click to toggle source

When resizing images, it is necessary to flatten any layers, otherwise the background may be completely black. This happens especially with PDFs. See #110

# File lib/bpl/derivatives/processors/image.rb, line 21
def create_resized_image
  create_image do |xfrm|
    if size
      xfrm.flatten
      xfrm.resize(size)
    end
  end
end
load_image_transformer() click to toggle source

Override this method if you want a different transformer, or need to load the raw image from a different source (e.g. external file)

# File lib/bpl/derivatives/processors/image.rb, line 48
def load_image_transformer
  MiniMagick::Image.open(source_path)
end
write_image(xfrm) click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 39
def write_image(xfrm)
  output_io = StringIO.new
  xfrm.write(output_io)
  output_io.rewind
  finalize_derivative_output(output_io.read)
end

Private Instance Methods

density() click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 62
def density
  directives.fetch(:density, nil)
end
quality() click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 58
def quality
  directives.fetch(:quality, nil)
end
selected_layers(image) click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 66
def selected_layers(image)
  if image.type =~ /pdf/i
    image.layers[directives.fetch(:layer, 0)]
  elsif directives.fetch(:layer, false)
    image.layers[directives.fetch(:layer)]
  else
    image
  end
end
size() click to toggle source
# File lib/bpl/derivatives/processors/image.rb, line 54
def size
  directives.fetch(:size, nil)
end