class DynamicImage::ImageProcessor

ImageProcessor

This is the image processing pipeline.

Example:

DynamicImage::ImageProcessor
  .new(file)
  .crop(crop_start, crop_size)
  .resize(size)
  .convert(:jpeg)
  .read

Attributes

image[R]
target_format[R]

Public Class Methods

new(image, target_format: nil) click to toggle source
# File lib/dynamic_image/image_processor.rb, line 27
def initialize(image, target_format: nil)
  if image.is_a?(Vips::Image)
    @image = image
    @target_format = target_format
  else
    reader = DynamicImage::ImageReader.new(image)
    @image = screen_profile(reader.read.autorot)
    @target_format = reader.format
  end
end

Public Instance Methods

convert(new_format) click to toggle source

Convert the image to a different format.

# File lib/dynamic_image/image_processor.rb, line 39
def convert(new_format)
  unless new_format.is_a?(DynamicImage::Format)
    new_format = DynamicImage::Format.find(new_format)
  end
  if frame_count > 1 && !new_format.animated?
    self.class.new(extract_frame(0), target_format: new_format)
  else
    self.class.new(image, target_format: new_format)
  end
end
read() click to toggle source

Returns the image data as a binary string.

# File lib/dynamic_image/image_processor.rb, line 51
def read
  tempfile = Tempfile.new(["dynamic_image", target_format.extension],
                          binmode: true)
  tempfile.close
  write(tempfile.path)
  tempfile.open
  tempfile.read
ensure
  tempfile.close
end
size() click to toggle source

Returns the image size as a Vector2d.

# File lib/dynamic_image/image_processor.rb, line 63
def size
  Vector2d.new(
    image.get("width"),
    image.get(
      image.get_fields.include?("page-height") ? "page-height" : "height"
    )
  )
end
write(path) click to toggle source

Write the image to a file.

# File lib/dynamic_image/image_processor.rb, line 73
def write(path)
  image.write_to_file(path, **target_format.save_options)
end

Private Instance Methods

apply(new_image) click to toggle source
# File lib/dynamic_image/image_processor.rb, line 79
def apply(new_image)
  self.class.new(new_image, target_format:)
end
blank_image() click to toggle source
# File lib/dynamic_image/image_processor.rb, line 83
def blank_image
  image.draw_rect([0.0, 0.0, 0.0, 0.0],
                  0, 0, image.get("width"), image.get("height"),
                  fill: true)
end