module AttachmentSaver::Processors::Image::Operations

Public Instance Methods

cover_and_crop(new_width, new_height, &block) click to toggle source

scales the image proportionately to fit over the given width and height (as for scale_to_cover), then crops the image to the given width & height. yields this image itself if it is already the appropriate size.

# File lib/processors/image.rb, line 207
def cover_and_crop(new_width, new_height, &block)
  scale_to_cover(new_width, new_height) do |scaled|
    return block.call(scaled) if new_width == scaled.width && new_height == scaled.height
    scaled.crop_to(new_width || width, new_height || height, &block)
  end
end
expand_to_fit(new_width, new_height, &block) click to toggle source

keeps proportions, as for scale_to_fit, but only ever makes images bigger. yields this image itself if it is already within the given dimensions or if the scaled dimensions would be smaller than the current dimensions. this is one of the operations specified by the *magick geometry strings, but IMHO it's not particularly useful as it doesn't establish any particularly helpful postconditions; consider whether scale_to_cover would be more appropriate.

# File lib/processors/image.rb, line 182
def expand_to_fit(new_width, new_height, &block)
  new_width, new_height = scale_dimensions_to_fit(new_width, new_height)
  return block.call(self) if new_width <= width && new_height <= height
  squish(new_width, new_height, &block)
end
scale_by(width_factor, height_factor, &block) click to toggle source

scales the image by the given factors.

# File lib/processors/image.rb, line 145
def scale_by(width_factor, height_factor, &block)
  squish(width*width_factor, height*height_factor, &block)
end
scale_dimensions_to_fit(new_width, new_height) click to toggle source

calculates the appropriate dimensions for scale_to_fit.

# File lib/processors/image.rb, line 150
def scale_dimensions_to_fit(new_width, new_height)
  raise ArgumentError, "must supply the width and/or height" if new_width.nil? && new_height.nil?
  if new_height.nil? || (!new_width.nil? && height*new_width < width*new_height)
    return [new_width, height*new_width/width]
  else
    return [width*new_height/height, new_height]
  end
end
scale_to_cover(new_width, new_height, &block) click to toggle source

scales the image proportionately so that it fits over the given width and height (ie. one dimension will be equal to the given dimension, and the other dimension will be larger than the given other dimension). either (but not both) of the new width & height may be nil, in which case the image will be scaled solely based on the other parameter (in this case the result is the same as using scale_to_fit). yields this image itself if it is already the appropriate size.

# File lib/processors/image.rb, line 194
def scale_to_cover(new_width, new_height, &block)
  raise ArgumentError, "must supply the width and/or height" if new_width.nil? && new_height.nil?
  if new_height.nil? || (!new_width.nil? && height*new_width > width*new_height)
    squish(new_width, height*new_width/width, &block)
  else
    squish(width*new_height/height, new_height, &block)
  end
end
scale_to_fit(new_width, new_height, &block) click to toggle source

scales the image proportionately so that it fits within the given width and height (ie. one dimension will be equal to the given dimension, and the other dimension will be smaller than the given other dimension). either (but not both) of the new width & height may be nil, in which case the image will be scaled solely based on the other parameter. yields this image itself if it is already the appropriate size.

# File lib/processors/image.rb, line 164
def scale_to_fit(new_width, new_height, &block)
  squish(*scale_dimensions_to_fit(new_width, new_height), &block)
end
shrink_to_fit(new_width, new_height, &block) click to toggle source

keeps proportions, as for scale_to_fit, but only ever makes images smaller. yields this image itself if it is already within the given dimensions.

# File lib/processors/image.rb, line 170
def shrink_to_fit(new_width, new_height, &block)
  new_width, new_height = scale_dimensions_to_fit(new_width, new_height)
  return block.call(self) if new_width >= width && new_height >= height
  squish(new_width, new_height, &block)
end
squish(new_width, new_height, &block) click to toggle source

squishes the image to the given width and height, without preserving the aspect ratio. yields this image itself if it is already the given size.

# File lib/processors/image.rb, line 139
def squish(new_width, new_height, &block)
  return block.call(self) if new_width == width && new_height == height
  resize_to(new_width.to_i, new_height.to_i, &block)
end