module DynamicImage::Model::Transformations

DynamicImage Model Transformations

Public Instance Methods

resize(max_size) click to toggle source

Resizes the image

# File lib/dynamic_image/model/transformations.rb, line 9
def resize(max_size)
  transform_image do |image|
    new_size = real_size.constrain_both(max_size)
    scale = new_size.x / real_size.x
    crop_attributes.each do |attr|
      self[attr] = self[attr] * scale if self[attr]
    end
    image.resize(new_size)
  end
end
rotate(degrees = 90) click to toggle source

Rotates the image

# File lib/dynamic_image/model/transformations.rb, line 21
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

Private Instance Methods

crop_attributes() click to toggle source
# File lib/dynamic_image/model/transformations.rb, line 39
def crop_attributes
  %i[crop_width crop_height crop_start_x crop_start_y
     crop_gravity_x crop_gravity_y]
end
rotate_dimensions(width, height, degrees) click to toggle source
# File lib/dynamic_image/model/transformations.rb, line 44
def rotate_dimensions(width, height, degrees)
  (degrees / 90).times do
    width, height = height, width

    self.real_width = width
    self.real_height = height

    self.crop_gravity_x, self.crop_gravity_y = rotated_crop_gravity(width)

    next unless cropped?

    self.crop_start_x, self.crop_start_y,
    self.crop_width, self.crop_height = rotated_crop(width)
  end
end
rotated_crop(new_width) click to toggle source
# File lib/dynamic_image/model/transformations.rb, line 60
def rotated_crop(new_width)
  return nil unless cropped?

  [
    new_width - (crop_start_y + crop_height),
    crop_start_x,
    crop_height,
    crop_width
  ]
end
rotated_crop_gravity(new_width) click to toggle source
# File lib/dynamic_image/model/transformations.rb, line 71
def rotated_crop_gravity(new_width)
  return nil unless crop_gravity?

  [new_width - crop_gravity_y, crop_gravity_x]
end
transform_image(&block) click to toggle source
# File lib/dynamic_image/model/transformations.rb, line 77
def transform_image(&block)
  read_image_metadata if data_changed?
  self.data = block.call(DynamicImage::ImageProcessor.new(data)).read
  read_image_metadata
  self
end