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. The crop is scaled along with it.

The image is scaled to fit within max_size, retaining its aspect ratio. Images smaller than max_size will be scaled up.

Parameters:

  • max_size (Vector2d, String) —

    the size to fit within, either a vector or a "{width}x{height}" string. Either dimension may be omitted to scale by the other alone.

Returns:

  • (self)

Raises:



23
24
25
26
27
28
29
# File 'lib/dynamic_image/model/transformations.rb', line 23

def resize(max_size)
  transform_image do |image|
    resized = image.resize_exact(fit_size(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:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dynamic_image/model/transformations.rb', line 41

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