Class: DynamicImage::ImageSizing
- Inherits:
-
Object
- Object
- DynamicImage::ImageSizing
- Defined in:
- lib/dynamic_image/image_sizing.rb
Overview
DynamicImage Image Sizing
Calculates cropping and fitting for image sizes. The helpers use it to work out the dimensions of a rendered image, and it can be used on its own when you need the size without rendering anything: reserving space in a layout, filling in og:image:width, or laying out a PDF.
The size it returns is the size that gets rendered. The helper floors it into the URL and the markup, and
DynamicImage::ImageProcessor::Transform#resize_exact honors that number instead of fitting it again. Scaling
is done in floating point, so snap pulls an axis that lands a rounding error below a whole pixel back onto it
before the helper's floor can drop the pixel.
Instance Method Summary collapse
-
#available_width(ratio = nil) ⇒ Integer
Returns the widest the image can be rendered at, in pixels.
-
#crop_geometry(ratio_vector) ⇒ Array(Vector2d, Vector2d)
Calculates crop geometry.
-
#fit(fit_size, options = {}) ⇒ Vector2d
Adjusts
fit_sizeto fit the image dimensions. -
#fit_renderable(fit_size, options = {}) ⇒ Vector2d
Fits the size like #fit, but returns the smallest size the image can be rendered at rather than raising when the result lands under a pixel.
-
#initialize(record, options = {}) ⇒ ImageSizing
constructor
A new instance of ImageSizing.
-
#renderable?(fit_size, options = {}) ⇒ Boolean
Returns true if the image can be rendered at
fit_size, false if #fit rejects it.
Constructor Details
#initialize(record, options = {}) ⇒ ImageSizing
Returns a new instance of ImageSizing.
22 23 24 25 |
# File 'lib/dynamic_image/image_sizing.rb', line 22 def initialize(record, = {}) @record = record @uncropped = [:uncropped] ? true : false end |
Instance Method Details
#available_width(ratio = nil) ⇒ Integer
Returns the widest the image can be rendered at, in pixels.
Without a ratio this is the image's own width. With one it is the width of the largest crop matching that ratio.
73 74 75 76 77 78 |
# File 'lib/dynamic_image/image_sizing.rb', line 73 def available_width(ratio = nil) ratio = DynamicImage::Ratio.parse(ratio) return size.x.floor unless ratio crop_geometry(Vector2d.new(ratio, 1)).first.x.floor end |
#crop_geometry(ratio_vector) ⇒ Array(Vector2d, Vector2d)
Calculates crop geometry. The given vector is scaled to match the image size, since cropping happens before resizing.
The crop is positioned to keep the record's crop gravity as close to the center as possible, clamped to the bounds of the image.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dynamic_image/image_sizing.rb', line 43 def crop_geometry(ratio_vector) require_nonzero!(ratio_vector) # Maximize the crop area to fit the image size crop_size = ratio_vector.fit(size).round # Ignore pixels outside the pre-cropped area for now center = crop_gravity - crop_start start = center - (crop_size / 2).floor start = clamp(start, crop_size, size) [crop_size, (start + crop_start)] end |
#fit(fit_size, options = {}) ⇒ Vector2d
Adjusts fit_size to fit the image dimensions. Any dimension set to zero will be ignored.
108 109 110 111 112 113 114 115 |
# File 'lib/dynamic_image/image_sizing.rb', line 108 def fit(fit_size, = {}) fit_size = parse_vector(fit_size) require_dimensions!(fit_size) if [:crop] require_nonzero!(fit_size) fit_size = snap(scale(fit_size, )) require_pixels!(fit_size) fit_size end |
#fit_renderable(fit_size, options = {}) ⇒ Vector2d
Fits the size like #fit, but returns the smallest size the image can be rendered at rather than raising when the result lands under a pixel.
131 132 133 134 135 136 |
# File 'lib/dynamic_image/image_sizing.rb', line 131 def fit_renderable(fit_size, = {}) require_nonzero!(parse_vector(fit_size)) return fit(fit_size, ) if [:crop] || renderable?(fit_size, ) size.cover(1).round end |
#renderable?(fit_size, options = {}) ⇒ Boolean
Returns true if the image can be rendered at fit_size, false if #fit rejects it.
150 151 152 153 154 155 156 |
# File 'lib/dynamic_image/image_sizing.rb', line 150 def renderable?(fit_size, = {}) vector = parse_vector(fit_size) return false if [:crop] && !(vector.x.positive? && vector.y.positive?) return false if vector.x.zero? && vector.y.zero? pixels?(snap(scale(vector, ))) end |