30

Using CarrierWave in Rails 3.0 how would you go about making the code resize images that have a width larger than 500 pixels to be 500 pixels wide and adjust the height appropriately - keeping the same initial ratio of width to height.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

4 Answers4

50

You Can use:

process :resize_to_limit => [500, nil]
William Wong Garay
  • 1,921
  • 18
  • 14
30
process :resize_to_limit => [500, 0]

This will process the image to be no wider than 500px while retaining the proper aspect ratio and allowing any height.

Jason Yost
  • 4,807
  • 7
  • 42
  • 65
  • 6
    Using this with carrierwave 0.10.0 and mini_magic 3.6.0 resulted in images scaled down to 1x1 pixels. Using `[500, nil]` as arguments worked for me instead. – efatsi Sep 02 '14 at 15:43
4

I know this is an old question, but I needed something similar.

I wanted images to be resized if they were larger than a given size, but not scaled up if they were smaller.

resize_to_limit(width, height)

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.

Details: http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/MiniMagick.html#M000051

Galaxy
  • 3,370
  • 3
  • 29
  • 41
1

If you want to limit the width only, use:

process :resize_to_limit => [500, -1]

and use:

process :resize_to_limit => [-1, 500]

to limit height only.

smoothdvd
  • 2,389
  • 4
  • 20
  • 25