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 Method Summary collapse
-
#content_type ⇒ Object
Returns the content type of the processed image.
-
#cropped_and_resized(size) ⇒ Object
Crops and resizes the image.
-
#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.
9 10 11 12 13 14 |
# File 'lib/dynamic_image/processed_image.rb', line 9 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 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'
25 26 27 |
# File 'lib/dynamic_image/processed_image.rb', line 25 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.
37 38 39 40 41 |
# File 'lib/dynamic_image/processed_image.rb', line 37 def cropped_and_resized(size) return crop_and_resize(size) unless record.persisted? find_or_create_variant(size).data end |
#normalized ⇒ Object
Normalizes the image.
-
Applies EXIF rotation
-
CMYK images are converted 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.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dynamic_image/processed_image.rb', line 57 def normalized require_valid_image! process_data do |image| image. do |combined| combined.auto_orient combined.colorspace("sRGB") if needs_colorspace_conversion? yield(combined) if block_given? optimize(combined) end image.format(format) if needs_format_conversion? end end |