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 Method Summary collapse

Constructor Details

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


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

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 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'

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
42
43
44
# File 'lib/dynamic_image/processed_image.rb', line 37

def cropped_and_resized(size)
  normalized do |image|
    if record.cropped? || size != record.size
      image.crop(image_sizing.crop_geometry_string(size))
      image.resize(size)
    end
  end
end

#normalizedObject

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.


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dynamic_image/processed_image.rb', line 60

def normalized
  require_valid_image!
  process_data do |image|
    image.combine_options 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