Module: AttachmentSaver::Processors::Image::Operations

Instance Method Summary collapse

Instance Method Details

#cover_and_crop(new_width, new_height, &block) ⇒ Object

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.



178
179
180
181
182
183
# File 'lib/processors/image.rb', line 178

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) ⇒ Object

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.



153
154
155
156
157
# File 'lib/processors/image.rb', line 153

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) ⇒ Object

scales the image by the given factors.



116
117
118
# File 'lib/processors/image.rb', line 116

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) ⇒ Object

calculates the appropriate dimensions for scale_to_fit.

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
# File 'lib/processors/image.rb', line 121

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) ⇒ Object

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.

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
# File 'lib/processors/image.rb', line 165

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) ⇒ Object

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.



135
136
137
# File 'lib/processors/image.rb', line 135

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) ⇒ Object

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.



141
142
143
144
145
# File 'lib/processors/image.rb', line 141

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) ⇒ Object

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.



110
111
112
113
# File 'lib/processors/image.rb', line 110

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