Class: DynamicImage::ProcessedImage

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

Overview

DynamicImage Processed Image

Crops, resizes and converts images, and reads back the processed data.

Each processed size is stored as a variant, so subsequent calls for the same size are cheap.

Examples:

size = DynamicImage::ImageSizing.new(image).fit("800x800")
data = DynamicImage::ProcessedImage.new(image, format: :jpg)
                                   .cropped_and_resized(size)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ProcessedImage.

Options Hash (options):

  • :uncropped (Boolean) —

    Ignore any crop stored on the record

  • :format (Symbol, String) —

    Format to convert to. Defaults to the format of the stored image.



22
23
24
25
26
27
# File 'lib/dynamic_image/processed_image.rb', line 22

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

Instance Attribute Details

#record ⇒ DynamicImage::Model (readonly)



16
17
18
# File 'lib/dynamic_image/processed_image.rb', line 16

def record
  @record
end

Instance Method Details

#cropped_and_resized(size) ⇒ String

Crops, resizes and normalizes the image.

Examples:

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

Raises:



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) ⇒ DynamicImage::Variant

Returns the variant for the given size, creating it if it doesn't already exist.

Raises:



51
52
53
54
55
# File 'lib/dynamic_image/processed_image.rb', line 51

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

#find_variant(size) ⇒ DynamicImage::Variant?

Returns the variant for the given size, if one exists.

Variants whose data has gone missing from storage are destroyed and treated as absent.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dynamic_image/processed_image.rb', line 63

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

  variant = record.variants.find_by(variant_params(size))
  return nil unless variant

  if Dis::Storage.exists?(variant.class.dis_type, variant.content_hash)
    variant
  else
    variant.destroy
    nil
  end
end

#format ⇒ DynamicImage::Format

The format the image is rendered in. Defaults to the format of the stored image.



92
93
94
# File 'lib/dynamic_image/processed_image.rb', line 92

def format
  DynamicImage::Format.find(@format_name) || record_format
end

#normalized {|image| ... } ⇒ String

Normalizes the image.

  • Applies EXIF rotation
  • Converts to sRGB
  • Strips metadata
  • Optimizes GIFs
  • Performs format conversion if the requested format is different

Examples:

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

Yields:

  • (image) —

    an optional block to transform the image before it is written out

Yield Parameters:

Yield Returns:

Raises:



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dynamic_image/processed_image.rb', line 114

def normalized
  require_valid_image!

  record.with_data_file do |path|
    image = DynamicImage::ImageProcessor.new(path)
    image = yield(image) if block_given?
    image.convert(format).read
  end
rescue Vips::Error => e
  raise DynamicImage::Errors::InvalidImage, e.message
end

#variant_for(size) ⇒ DynamicImage::Variant?

Same as #cropped_and_resized, but returns the variant record instead of its data.



81
82
83
84
85
86
87
# File 'lib/dynamic_image/processed_image.rb', line 81

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

  find_or_create_variant(size)
rescue ActiveRecord::RecordNotUnique
  find_variant(size)
end