Search code examples
ruby-on-railsrubyimageattachment-fu

How do you validate attachment_fu's image width and height?


I want to be able to validate the image is exactly a certain with or a certain height, or if it's square.

In the validation block of the model that has_attachment, when I try to access image_size, width, or height, it always comes out as null.

I also asked the question here if you want more details.


Solution

  • Yea, you need to hack a bit in order to get it to work, but not so much. Adapting from attachment_fu's own image processor:

     validate :validate_image_size
    
      private
    
      def validate_image_size
        w, h = width, height
    
        unless w or h
          with_image do |img|
            w, h = img.columns, img.rows
          end
        end
    
        errors.add(:width, "must less than 250px")  if w > 250
        errors.add(:height, "must less than 250px")  if h > 250
      end
    end