在我尝试自己完成之后,我环顾四周并找不到解决方案。当用户上传照片时,如果超过我的最小和最大尺寸,我希望调整它们的大小。但是我想要两个条件。侧面拍摄的照片(东/西)应保持在我设定的尺寸范围内,高照度拍摄的照片(北/南)也是如此。
例如,用户上传的图片长期存在,尺寸为3264x1840。上传应调整大小以适应584x329。如果上传小于584x329,则不会调整大小。
另一个例子是,如果用户上传了一张高大的照片并且尺寸为2448 x 3264.上传内容应调整为适合247x329。
我试图使用MiniMagick,因为我认为这是必需的。如果我只能使用CarrierWave那么这是完美的,但我认为MiniMagick应该用于调整照片大小。
我收到的错误是来自控制器def create的'undefined method resize' for #<ImageUploader:0x007f8606feb9b8>' and it points to
@ photo = Photo.new(params [:photo])`。
BTW尺寸很高,因为当您上传照片时,这些通常是您手机的默认尺寸。
image_uploader.rb:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :resize => [584, 329]
def resize_to_limit(width, height)
manipulate! do |img|
img.resize "#{width}x#{height}>"
img = yield(img) if block_given?
img
end
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_limit => [200, 200]
end
end
照片控制器:
def create
@photo = Photo.new(params[:photo])
@photo.user = current_user
if @photo.save
flash[:notice] = "Successfully created photos."
redirect_to :back
else
render :action => 'new'
end
end
def resize(width, height, gravity = 'Center')
manipulate! do |img|
img.combine_options do |cmd|
cmd.resize "#{width}"
if img[:width] < img[:height]
cmd.gravity gravity
cmd.background "rgba(255,255,255,0.0)"
cmd.extent "#{width}x#{height}"
end
end
img = yield(img) if block_given?
img
end
end
答案 0 :(得分:2)
将图片上传器中的:resize
更改为process :resize_to_fit => [584, 329]