Module: DynamicImage::Model::Transformations

Included in:
DynamicImage::Model
Defined in:
lib/dynamic_image/model/transformations.rb

Overview

DynamicImage Model Transformations

Transformations that rewrite the stored file. Per-request processing never touches it.

Both methods replace the data and update the stored dimensions, adjusting the crop to match. Neither saves the record.

Instance Method Summary collapse

Instance Method Details

#resize(max_size) ⇒ self

Resizes the image, replacing the stored file with a smaller one. The crop is scaled along with it.

Parameters:

  • max_size (Vector2d)

    the size to scale down to

Returns:

  • (self)


16
17
18
19
20
21
22
# File 'lib/dynamic_image/model/transformations.rb', line 16

def resize(max_size)
  transform_image do |image|
    resized = image.resize(real_size.constrain_both(max_size))
    scale_crop(resized.size)
    resized
  end
end

#rotate(degrees = 90) ⇒ self

Rotates the image, taking the crop and crop gravity with it.

Examples:

image.rotate(90)
image.save

Parameters:

  • degrees (Integer) (defaults to: 90)

    the angle, which must be a multiple of 90. Rotating by 0 is a no-op.

Returns:

  • (self)

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dynamic_image/model/transformations.rb', line 33

def rotate(degrees = 90)
  degrees = degrees.to_i % 360

  return self if degrees.zero?

  if (degrees % 90).nonzero?
    raise DynamicImage::Errors::InvalidTransformation,
          "angle must be a multiple of 90 degrees"
  end

  transform_image do |image|
    rotate_dimensions(real_size.x, real_size.y, degrees)
    image.rotate(degrees)
  end
end