Class: DynamicImage::ProcessedImage
- Inherits:
-
Object
- Object
- DynamicImage::ProcessedImage
- 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.
Instance Attribute Summary collapse
- #record ⇒ DynamicImage::Model readonly
Instance Method Summary collapse
-
#cropped_and_resized(size) ⇒ String
Crops, resizes and normalizes the image.
-
#find_or_create_variant(size) ⇒ DynamicImage::Variant
Returns the variant for the given size, creating it if it doesn't already exist.
-
#find_variant(size) ⇒ DynamicImage::Variant?
Returns the variant for the given size, if one exists.
-
#format ⇒ DynamicImage::Format
The format the image is rendered in.
-
#initialize(record, options = {}) ⇒ ProcessedImage
constructor
A new instance of ProcessedImage.
-
#normalized {|image| ... } ⇒ String
Normalizes the image.
-
#variant_for(size) ⇒ DynamicImage::Variant?
Same as #cropped_and_resized, but returns the variant record instead of its data.
Constructor Details
#initialize(record, options = {}) ⇒ ProcessedImage
Returns a new instance of ProcessedImage.
22 23 24 25 26 27 |
# File 'lib/dynamic_image/processed_image.rb', line 22 def initialize(record, = {}) @record = record @uncropped = [:uncropped] ? true : false @format_name = [:format].to_s.upcase if [: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.
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.
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
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. 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 |