Class: DynamicImage::ProcessedImage

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/processed_image.rb

Overview

DynamicImage Processed Image

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, options = {}) ⇒ ProcessedImage

Returns a new instance of ProcessedImage.



11
12
13
14
15
16
# File 'lib/dynamic_image/processed_image.rb', line 11

def initialize(record, options = {})
  @record    = record
  @uncropped = options[:uncropped] ? true : false
  @format    = options[:format].to_s.upcase if options[:format]
  @format    = "JPEG" if defined?(@format) && @format == "JPG"
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



9
10
11
# File 'lib/dynamic_image/processed_image.rb', line 9

def record
  @record
end

Instance Method Details

#content_typeObject

Returns the content type of the processed image.

Example

image = Image.find(params[:id])
DynamicImage::ProcessedImage.new(image).content_type
# => 'image/png'
DynamicImage::ProcessedImage.new(image, :jpeg).content_type
# => 'image/jpeg'


27
28
29
# File 'lib/dynamic_image/processed_image.rb', line 27

def content_type
  "image/#{format}".downcase
end

#cropped_and_resized(size) ⇒ Object

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.



39
40
41
42
43
# File 'lib/dynamic_image/processed_image.rb', line 39

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) ⇒ Object

Find or create a variant with the given size.



46
47
48
49
50
# File 'lib/dynamic_image/processed_image.rb', line 46

def find_or_create_variant(size)
  find_variant(size) || create_variant(size)
rescue ActiveRecord::RecordNotUnique
  find_variant(size)
end

#find_variant(size) ⇒ Object

Find a variant with the given size.



53
54
55
56
57
# File 'lib/dynamic_image/processed_image.rb', line 53

def find_variant(size)
  return nil unless record.persisted?

  record.variants.find_by(variant_params(size))
end

#normalizedObject

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.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dynamic_image/processed_image.rb', line 73

def normalized
  require_valid_image!
  process_data do |image|
    image.combine_options do |combined|
      combined.auto_orient
      convert_to_srgb(image, combined)
      yield(combined) if block_given?
      optimize(combined)
    end
    image.format(format) if needs_format_conversion?
  end
end