class DynamicImage::ProcessedImage

DynamicImage Processed Image

Handles all processing of images. Takes an instance of DynamicImage::Model as argument.

Attributes

record[R]

Public Class Methods

new(record, options = {}) click to toggle source
# File lib/dynamic_image/processed_image.rb, line 11
def initialize(record, options = {})
  @record    = record
  @uncropped = options[:uncropped] ? true : false
  @format_name = options[:format].to_s.upcase if options[:format]
  @format_name = "JPEG" if defined?(@format_name) && @format_name == "JPG"
end

Public Instance Methods

cropped_and_resized(size) click to toggle source

Crops and resizes the image. Normalization is performed as well.

Example

processed = DynamicImage::ProcessedImage.new(image)
image_data = processed.cropped_and_resized(Vector2d.new(200, 200))

Returns a binary string.

# File lib/dynamic_image/processed_image.rb, line 26
def cropped_and_resized(size)
  return crop_and_resize(size) unless record.persisted?

  find_or_create_variant(size).data
end
find_or_create_variant(size) click to toggle source

Find or create a variant with the given size.

# File lib/dynamic_image/processed_image.rb, line 33
def find_or_create_variant(size)
  find_variant(size) || create_variant(size)
rescue ActiveRecord::RecordNotUnique
  find_variant(size)
end
find_variant(size) click to toggle source

Find a variant with the given size.

# File lib/dynamic_image/processed_image.rb, line 40
def find_variant(size)
  return nil unless record.persisted?

  record.variants.find_by(variant_params(size))
end
format() click to toggle source
# File lib/dynamic_image/processed_image.rb, line 46
def format
  DynamicImage::Format.find(@format_name) || record_format
end
normalized() { |image| ... } click to toggle source

Normalizes the image.

  • Applies EXIF rotation

  • Converts to sRGB

  • Strips metadata

  • Optimizes GIFs

  • Performs format conversion if the requested format is different

Example

processed = DynamicImage::ProcessedImage.new(image, :jpeg)
jpg_data = processed.normalized

Returns a binary string.

# File lib/dynamic_image/processed_image.rb, line 64
def normalized
  require_valid_image!

  image = DynamicImage::ImageProcessor.new(record.data)
  image = yield(image) if block_given?
  image.convert(format).read
end

Private Instance Methods

create_variant(size) click to toggle source
# File lib/dynamic_image/processed_image.rb, line 74
def create_variant(size)
  record.variants.create(
    variant_params(size).merge(filename: record.filename,
                               content_type: format.content_type,
                               data: crop_and_resize(size))
  )
end
crop_and_resize(size) click to toggle source
# File lib/dynamic_image/processed_image.rb, line 82
def crop_and_resize(size)
  normalized do |image|
    image.crop(*image_sizing.crop_geometry(size)).resize(size)
  end
end
image_sizing() click to toggle source
# File lib/dynamic_image/processed_image.rb, line 88
def image_sizing
  @image_sizing ||=
    DynamicImage::ImageSizing.new(record, uncropped: @uncropped)
end
record_format() click to toggle source
# File lib/dynamic_image/processed_image.rb, line 93
def record_format
  DynamicImage::Format.content_type(record.content_type)
end
require_valid_image!() click to toggle source
# File lib/dynamic_image/processed_image.rb, line 97
def require_valid_image!
  raise DynamicImage::Errors::InvalidImage unless record.valid?
end
variant_params(size) click to toggle source
# File lib/dynamic_image/processed_image.rb, line 101
def variant_params(size)
  crop_size, crop_start = image_sizing.crop_geometry(size)

  { width: size.x.round, height: size.y.round,
    crop_width: crop_size.x, crop_height: crop_size.y,
    crop_start_x: crop_start.x, crop_start_y: crop_start.y,
    format: format.name }
end