【发布时间】:2018-06-16 21:41:02
【问题描述】:
我安装了carrierwave 和minimagick,我尝试调整图像的大小,如果它是纵向的还是横向的,我遵循了carrierwave 文档,但是有些东西不起作用,它正在创建纵向和横向图像。我已经玩了一段时间了,我完全没有想法!任何想法将不胜感激。
这是我的代码: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 个。
提前致谢!
【问题讨论】:
标签: ruby-on-rails ruby carrierwave image-resizing