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.



205
206
207
208
209
210
# File 'lib/processors/image.rb', line 205

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.



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

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.



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

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)


148
149
150
151
152
153
154
155
# File 'lib/processors/image.rb', line 148

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)


192
193
194
195
196
197
198
199
# File 'lib/processors/image.rb', line 192

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.



162
163
164
# File 'lib/processors/image.rb', line 162

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.



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

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.



137
138
139
140
# File 'lib/processors/image.rb', line 137

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