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.



207
208
209
210
211
212
# 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) ⇒ 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.



182
183
184
185
186
# 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) ⇒ Object

scales the image by the given factors.



145
146
147
# 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) ⇒ Object

calculates the appropriate dimensions for scale_to_fit.

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
# 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) ⇒ 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)


194
195
196
197
198
199
200
201
# 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) ⇒ 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.



164
165
166
# 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) ⇒ 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.



170
171
172
173
174
# 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) ⇒ 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.



139
140
141
142
# 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