CarrierWave并调整大小到特定宽度

时间:2022-11-19 12:56:53

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.

在Rails 3.0中使用CarrierWave如何使代码调整宽度大于500像素的图像大小为500像素宽并适当调整高度 - 保持宽度与高度的初始比率相同。

5 个解决方案

#1


38  

You Can use:

您可以使用:

process :resize_to_limit => [500, nil]

#2


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.

这将处理图像不超过500px,同时保持适当的宽高比并允许任何高度。

#3


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_to_limit(宽度,高度)

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

详细信息:http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/MiniMagick.html#M000051

#4


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.

仅限高度。

#5


-4  

Assuming you are using RMagick or MiniMagick, add this line to your uploader class:

假设您使用的是RMagick或MiniMagick,请将此行添加到您的上传器类:

process :resize_to_fit => [500]

More info:

更多信息:

http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/RMagick.html#M000064 http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/RMagick.html#M000064 http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

#1


38  

You Can use:

您可以使用:

process :resize_to_limit => [500, nil]

#2


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.

这将处理图像不超过500px,同时保持适当的宽高比并允许任何高度。

#3


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_to_limit(宽度,高度)

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

详细信息:http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/MiniMagick.html#M000051

#4


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.

仅限高度。

#5


-4  

Assuming you are using RMagick or MiniMagick, add this line to your uploader class:

假设您使用的是RMagick或MiniMagick,请将此行添加到您的上传器类:

process :resize_to_fit => [500]

More info:

更多信息:

http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/RMagick.html#M000064 http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/RMagick.html#M000064 http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit