mat*_*sfh 3 ruby-on-rails-3 carrierwave minimagick
只有当图像大于版本的大小时,才能使用carrierwave创建一个版本(例如拇指)?
例:
version :thumb, :if => :is_thumbnable? do
process :resize_to_fit => [32,nil]
end
protected
def is_thumbnable?(file)
image ||= MiniMagick::Image.open(file.path)
if image.nil?
if image['width'] >= 32 || image['height'] >= 32
true
else
false
end
else
false
end
end
Run Code Online (Sandbox Code Playgroud)
我尝试过它们并不适合我.在调整开发中的大图像时,我会阻止服务器.
所以我看看文档:http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/RMagick.html
有一个很棒的功能: resize_to_limit(width, height)
调整图像大小以适合指定的尺寸,同时保留原始高宽比.仅当图像大于指定尺寸时才会调整图像大小.生成的图像可能比较小尺寸中指定的更短或更窄,但不会大于指定值.
我的代码看起来像这样:
version :version_name, from_version: :parent_version_name do
process resize_to_limit: [width, nil]
end
Run Code Online (Sandbox Code Playgroud)
只有当它更大时才调整大小以适应宽度,尊重w/h的比率.