我正在对上传的图像进行简单的旋转+调整大小,但前提是它是横向格式。否则,我只想调整图像大小。我也想在保持版本名称相同的同时执行此操作(没有“medium”和“medium_rotated”)到目前为止我有旋转工作但问题是如果我上传非横向图像,它不会工作。它仅适用于风景图像。到目前为止,这是我的代码的相关部分。有任何想法吗?
-本尼
class FloorPlanPhotoUploader < CarrierWave::Uploader::Base
....
version :medium, :if => :is_landscape? do
process :rotate_cw
end
version :medium do
process :resize_and_pad => [ 260, 360, :white, 'Center']
end
def is_landscape? picture
file = (picture.is_a? CarrierWave::Storage::Fog::File) ? picture.public_url : picture.file
image = MiniMagick::Image.open(file)
image[:width] > image[:height]
end
def rotate_cw
manipulate! do |img|
img.rotate "90>"
img = yield(img) if block_given?
img
end
end
....
end
最佳答案
问题是您已经定义了 :medium
版本两次。它击中了:
..., :if => is_landscape?
部分,对于非横向图像返回 false。结果,什么也没做。
version :medium
的第二次声明你已经到了那里永远不会运行,因为你不能声明两个具有相同名称的版本,所以它完全被跳过了。您需要做的只是生成一个版本,名为
:medium
,并有条件地处理顺时针旋转。就像是:class FloorPlanPhotoUploader < CarrierWave::Uploader::Base
...
version :medium do
process :rotate_cw, :if => :is_landscape?
process :resize_and_pad => [ 260, 360, :white, 'Center']
end
...
end
您可以通过这种方式在单个版本中链接多个处理步骤。这里is a great tutorial这将更深入地讨论这个主题。
关于Carrierwave 中的条件版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9795664/