在载波上传器中验证图像大小



所有上传的图片应该至少为150 × 150像素。如何用载波验证它?

为什么不使用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的答案做了一个稍微完整一点的验证器

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}一样使用

让我惊讶的是,寻找一种明确的方法来验证图像宽度是多么困难。高度与载波。@Kir上面的解决方案是正确的,但我想进一步解释他所做的事情,以及我所做的微小改变。

如果你看他的gist 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来获取想要的属性,但只是不想将它们持久化到db中。所以我的魔法行变成了

model.image_width, model.image_height = `identify -format "%wx %h" #{new_file.path}`.split(/x/).map { |dim| dim.to_i }

现在我有了宽度&存储在模型对象中的图像的高度。最后一步是为维度编写自定义验证,因此在模型中需要类似

的内容。
validate :validate_minimum_image_size

然后在它下面定义自定义验证方法,与要点

相同
# 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

我刚刚做了一个自定义验证器,旨在使Rails 4+语法更友好。
我从这个帖子上的其他人的回复中得到了一些想法。
以下是要点: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 }

最新更新