Class: DynamicImage::ProcessedImage
- Inherits:
-
Object
- Object
- DynamicImage::ProcessedImage
- 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
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
-
#content_type ⇒ Object
Returns the content type of the processed image.
-
#cropped_and_resized(size) ⇒ Object
Crops and resizes the image.
-
#find_or_create_variant(size) ⇒ Object
Find or create a variant with the given size.
-
#find_variant(size) ⇒ Object
Find a variant with the given size.
-
#initialize(record, options = {}) ⇒ ProcessedImage
constructor
A new instance of ProcessedImage.
-
#normalized ⇒ Object
Normalizes the image.
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, = {}) @record = record @uncropped = [:uncropped] ? true : false @format = [:format].to_s.upcase if [:format] @format = "JPEG" if defined?(@format) && @format == "JPG" end |
Instance Attribute Details
#record ⇒ Object (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_type ⇒ Object
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 |
#normalized ⇒ Object
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. 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 |