0

Is there a way to make RMagick resize an image like this:

  • Resize so that both width and height are smaller than, say, 200px, but keep aspect ratio
  • If width > 200 and width > height, resize so that width = 200 and resize height accordingly
  • If height > 200 and height > width, resize so that height = 200 and resize width accordingly

EDIT: By the way, I am looking for this syntactic way of achieving this, and not API-based manipulation.

K Everest
  • 1,565
  • 5
  • 15
  • 23

1 Answers1

0

this method should work, image is an Rmagick image, width and height are integers (max size of the image)

def resize_to_limit(image,width, height)
  geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
  image.change_geometry!(geometry) do |new_width, new_height, img|
    img.resize!(new_width, new_height)
  end
  image
end

ex. i want to resize an image to 2000 if its bigger than that, preserving the aspect ratio

image = resize_to_limit(image,2000,2000)
Orlando
  • 9,374
  • 3
  • 56
  • 53