我安装了carrierwave和minimagick,我试图调整图像大小,如果它的肖像或它的风景,香港专业教育学院遵循运载波文档,但有些东西不起作用,它创建了肖像和风景图像。我现在已经玩了一段时间了,我想出了所有想法!任何想法都会受到赞赏。
这是我的代码my:uploader.rb
class CoverUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
# storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :landscape, if: :is_landscape?
version :landscape do
process resize_to_fit: [@land_height, 250]
end
version :portrait, if: :is_portrait?
version :portrait do
process resize_to_fit: [250, @port_width]
end
process :store_dimensions
def extension_whitelist
%w(jpg jpeg png)
end
private
def store_dimensions
if file && model
model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
end
end
def is_landscape? picture
image = MiniMagick::Image.open(picture.path)
image[:width] > image[:height]
width = image[:width]
aspect = image[:width] / image[:height].to_f
@height = aspect * width
end
def is_portrait? picture
image = MiniMagick::Image.open(picture.path)
image[:width] < image[:height]
height = image[:height]
aspect = image[:width] / image[:height].to_f
@width = height / aspect
end
end
所以这是创建3张图片而不是2张。
提前致谢!
答案 0 :(得分:0)
这里只是粗略猜测:你用相似但不同的块覆盖version
,而省略了你的条件。清理它。
version :portrait, :if => :is_portrait? do
process resize_to_fit: [250, @port_width]
end
version :landscape, :if => :is_landscape? do
process resize_to_fit: [@land_height, 250]
end