Module: DynamicImage::Model

Extended by:
ActiveSupport::Concern
Includes:
Dis::Model, Dimensions, Transformations, Validations, Variants
Defined in:
lib/dynamic_image/model.rb,
lib/dynamic_image/model/variants.rb,
lib/dynamic_image/model/dimensions.rb,
lib/dynamic_image/model/validations.rb,
lib/dynamic_image/model/transformations.rb

Overview

DynamicImage Model

ActiveModel extension for the model holding image data. The table needs at least the attributes in Schema::ATTRIBUTES:

create_table :images do |t|
t.string  :content_hash
t.string  :content_type
t.integer :content_length
t.string  :filename
t.string  :colorspace
t.integer :real_width, :real_height
t.integer :crop_width, :crop_height
t.integer :crop_start_x, :crop_start_y
t.integer :crop_gravity_x, :crop_gravity_y
t.timestamps
end

Include it in your model:

class Image < ActiveRecord::Base
include DynamicImage::Model
end

Usage

To save an image, assign to the file attribute. The image is parsed and validated when the record is saved.

image = Image.create(file: params.permit(:file))

To read back the image data, access the data attribute. The data is loaded lazily from the store.

data = image.data

Cropping

Images can be pre-cropped by setting crop_width, crop_height, crop_start_x and crop_start_y. The crop dimensions cannot exceed the image size.

image.update(
crop_start_x: 15, crop_start_y: 20,
crop_width: 300, crop_height: 200
)
image.size # => Vector2d(300, 200)

By default, images will be cropped from the center. You can control this by setting crop_gravity_x and crop_gravity_y. DynamicImage will make sure the pixel referred to by these coordinates are present in the cropped image, and as close to the center as possible without zooming in.

Defined Under Namespace

Modules: Dimensions, Transformations, Validations, Variants

Instance Method Summary collapse

Methods included from Transformations

#resize, #rotate

Methods included from Dimensions

#crop_gravity, #crop_gravity?, #crop_size, #crop_size?, #crop_start, #crop_start?, #cropped?, #real_size, #real_size?, #size, #size?

Instance Method Details

#alt_textString?

Returns the alt text for the image, or nil if none has been set.

DynamicImage doesn't add this column by default. Either create it yourself or override the method to provide your own implementation.

Note that there is a distinction between nil and a blank string. alt="" means the image is purely decorative, while a missing attribute is an accessibility defect.

Returns:

  • (String, nil)

See Also:



97
98
99
# File 'lib/dynamic_image/model.rb', line 97

def alt_text
  self[:alt_text] if has_attribute?(:alt_text)
end

#animated?Boolean

Returns true if the image holds more than one frame in a format that renders them.

Images stored before frame_count existed have none, and are taken to be still. A multi-page document in a format that isn't animated, such as TIFF, is also still: ImageReader loads only its first page.

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/dynamic_image/model.rb', line 81

def animated?
  return false unless has_attribute?(:frame_count) && frame_count.to_i > 1

  DynamicImage::Format.content_type(content_type)&.animated? || false
end

#cmyk?Boolean

Returns true if the image is in the CMYK colorspace.

Returns:

  • (Boolean)


104
105
106
# File 'lib/dynamic_image/model.rb', line 104

def cmyk?
  colorspace == "cmyk"
end

#gray?Boolean

Returns true if the image is in the grayscale colorspace.

Returns:

  • (Boolean)


111
112
113
# File 'lib/dynamic_image/model.rb', line 111

def gray?
  colorspace == "gray"
end

#rgb?Boolean

Returns true if the image is in the RGB colorspace.

Returns:

  • (Boolean)


118
119
120
# File 'lib/dynamic_image/model.rb', line 118

def rgb?
  colorspace == "rgb"
end

#safe_content_typeString

Finds a web safe content type, negotiated against DynamicImage#default_formats.

Returns:

  • (String)

See Also:



126
127
128
129
# File 'lib/dynamic_image/model.rb', line 126

def safe_content_type
  DynamicImage::FormatNegotiator
    .new(self).negotiate(DynamicImage.default_formats).content_type
end

#to_paramString

Includes a timestamp fingerprint in the URL param, so rendered images can be cached indefinitely.

Returns:

  • (String)

    the id and an updated_at fingerprint



134
135
136
# File 'lib/dynamic_image/model.rb', line 134

def to_param
  [id, updated_at.utc.to_fs(cache_timestamp_format)].join("-")
end