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.
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
-
#alt_text ⇒ String?
Returns the alt text for the image, or nil if none has been set.
-
#animated? ⇒ Boolean
Returns true if the image holds more than one frame in a format that renders them.
-
#cmyk? ⇒ Boolean
Returns true if the image is in the CMYK colorspace.
-
#gray? ⇒ Boolean
Returns true if the image is in the grayscale colorspace.
-
#rgb? ⇒ Boolean
Returns true if the image is in the RGB colorspace.
-
#safe_content_type ⇒ String
Finds a web safe content type, negotiated against #default_formats.
-
#to_param ⇒ String
Includes a timestamp fingerprint in the URL param, so rendered images can be cached indefinitely.
Methods included from Transformations
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_text ⇒ String?
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.
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.
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.
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.
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.
118 119 120 |
# File 'lib/dynamic_image/model.rb', line 118 def rgb? colorspace == "rgb" end |
#safe_content_type ⇒ String
Finds a web safe content type, negotiated against DynamicImage#default_formats.
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_param ⇒ String
Includes a timestamp fingerprint in the URL param, so rendered images can be cached indefinitely.
134 135 136 |
# File 'lib/dynamic_image/model.rb', line 134 def to_param [id, updated_at.utc.to_fs()].join("-") end |