Carrierwave - Resizing images to fixed width Carrierwave - Resizing images to fixed width ruby ruby

Carrierwave - Resizing images to fixed width


Just put this in your uploader file:

class ImageUploader < CarrierWave::Uploader::Base  version :resized do    # returns an image with a maximum width of 100px     # while maintaining the aspect ratio    # 10000 is used to tell CW that the height is free     # and so that it will hit the 100 px width first    process :resize_to_fit => [100, 10000]  endend

Documentation and example here: http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

Keep in mind, resize_to_fit will scale up images if they are smaller than 100px. If you don't want it to do that, then replace that with resize_to_limit.


I use

process :resize_to_fit => [100, 10000]

Use 10000 or any very big number to let Carrierwave know the height is free, just resize to the width.

@iWasRobbed: I don't think that's the correct solution. According to the link you pasted about resize_to_fit: The maximum height of the resized image. If omitted it defaults to the value of new_width. So in your case process :resize_to_fit => [100, nil] is equivalent to process :resize_to_fit => [100, 100] which doesn't guarantee that you will always get the fixed width of 100px


Wouldn't a better solution actually be:

process :resize_to_fit => [100, -1]

This way you don't have to limit height at all

EDIT: Just realized this only works with MiniMagick, For RMagick you seem to have no option but to add a large number to the height