在carrierwave上传器中validation图像大小
所有上传内容至少应为150×150像素。 如何使用Carrierwavevalidation它?
为什么不使用MiniMagick? 修改DelPiero的答案:
validate :validate_minimum_image_size def validate_minimum_image_size image = MiniMagick::Image.open(picture.path) unless image[:width] > 400 && image[:height] > 400 errors.add :image, "should be 400x400px minimum!" end end
我根据@ skalee的答案做了一个稍微完整的validation器
class ImageSizeValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless value.blank? image = MiniMagick::Image.open(value.path) checks = [ { :option => :width, :field => :width, :function => :'==', :message =>"Image width must be %d px."}, { :option => :height, :field => :height, :function => :'==', :message =>"Image height must be %d px."}, { :option => :max_width, :field => :width, :function => :'<=', :message =>"Image width must be at most %d px."}, { :option => :max_height, :field => :height, :function => :'<=', :message =>"Image height must be at most %d px."}, { :option => :min_width, :field => :width, :function => :'>=', :message =>"Image width must be at least %d px."}, { :option => :min_height, :field => :height, :function => :'>=', :message =>"Image height must be at least %d px."}, ] checks.each do |p| if options.has_key?(p[:option]) and !image[p[:field]].send(p[:function], options[p[:option]]) record.errors[attribute] << p[:message] % options[p[:option]] end end end end end
像validates :image, :image_size => {:min_width=>400, :min_height => 400}
一样使用它validates :image, :image_size => {:min_width=>400, :min_height => 400}
。
我的解决方案: https : //gist.github.com/1239078
让我感到惊讶的是,用CarrierWave搜索一个明确的方法来validation图像宽度和高度是多么困难。 上面@ Kir的解决方案是正确的,但我想更进一步解释他做了什么,以及我所做的微小改动。
如果你看看他的要点https://gist.github.com/1239078 ,答案就在于他在Uploader类中的before :cache
回调。 神奇的线条是
model.avatar_upload_width, model.avatar_upload_height = `identify -format "%wx %h" #{new_file.path}`.split(/x/).map { |dim| dim.to_i }
在他的情况下,avatar_upload_width和avatar_upload_height是他的User模型的属性。 我不想在数据库中存储宽度和高度,所以在我的模型中我说:
attr_accessor :image_width, :image_height
请记住,在处理记录时,您可以使用attr_accessor获取您想要拥有的属性,但只是不想将它们持久保存到数据库中。 所以我的魔术线实际上变成了
model.image_width, model.image_height = `identify -format "%wx %h" #{new_file.path}`.split(/x/).map { |dim| dim.to_i }
所以现在我将图像的宽度和高度存储在模型对象中。 最后一步是为尺寸编写自定义validation,因此在您的模型中需要类似的东西
validate :validate_minimum_image_size
然后在下面定义您的自定义validation方法,与要点相同
# custom validation for image width & height minimum dimensions def validate_minimum_image_size if self.image_width < 400 && self.image_height < 400 errors.add :image, "should be 400x400px minimum!" end end
我刚刚制作了一个自定义validation器,旨在提供更多Rails 4+语法友好性。
我在这个post中从其他人的回答中得到了想法。
这是要点: https : //gist.github.com/lou/2881f1aa183078687f1e
你可以像这样使用它:
validates :image, image_size: { width: { min: 1024 }, height: { in: 200..500 } }
在这种特殊情况下应该是:
validates :image, image_size: { width: 150, height: 150 }