Class: DynamicImage::ImageProcessor

Inherits:
Object
  • Object
show all
Includes:
Colors, Frames, Transform
Defined in:
lib/dynamic_image/image_processor.rb,
lib/dynamic_image/image_processor/colors.rb,
lib/dynamic_image/image_processor/frames.rb,
lib/dynamic_image/image_processor/transform.rb

Overview

ImageProcessor

This is the image processing pipeline.

Example:

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

Defined Under Namespace

Modules: Colors, Frames, Transform

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transform

#crop, #resize, #rotate

Methods included from Frames

#frame, #frame_count

Constructor Details

#initialize(image, target_format: nil) ⇒ ImageProcessor

Returns a new instance of ImageProcessor.



27
28
29
30
31
32
33
34
35
36
# 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

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



25
26
27
# File 'lib/dynamic_image/image_processor.rb', line 25

def image
  @image
end

#target_formatObject (readonly)

Returns the value of attribute target_format.



25
26
27
# File 'lib/dynamic_image/image_processor.rb', line 25

def target_format
  @target_format
end

Instance Method Details

#convert(new_format) ⇒ Object

Convert the image to a different format.



39
40
41
42
43
44
45
46
47
48
# 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

#readObject

Returns the image data as a binary string.



51
52
53
54
55
56
57
58
59
60
# 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

#sizeObject

Returns the image size as a Vector2d.



63
64
65
66
67
68
69
70
# 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) ⇒ Object

Write the image to a file.



73
74
75
# File 'lib/dynamic_image/image_processor.rb', line 73

def write(path)
  image.write_to_file(path, **target_format.save_options)
end