我安装了载波和minimagick,如果它的肖像或风景,我试图调整图像的大小,我遵循了载波文档,但有些东西不起作用,它正在创建纵向和横向图像。我现在玩了一段时间,我都没主意了!任何想法将不胜感激。
这是我的代码我的: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 个。
提前感谢!
在这里做一个疯狂的猜测:你用相似但不同的块覆盖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