1

Below is my validations for image content type which works fine.

validates_attachment_size :icon, :less_than => MAX_SIZE.megabytes, :message => "Max size is 1 mb"
validates_attachment_content_type :icon, :content_type => ['image/jpg','image/jpeg', 'image/png', 'image/gif']

BUT

I need to validate the dimensions also and my code is

validates_each :icon do |record, attr, value|
    if record.icon_file_name
       dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original])
       if(dimensions.width > 600 || dimensions.height > 400)
         record.errors.add(:file, " #{record.icon_file_name} dimensions must be less than or equal to 600*400")
       end
     end
   end

AND

it gives imagemagick error Not recognized by the 'identify' command error

Can you put some light on this?

Thanks.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
ashisrai_
  • 6,438
  • 2
  • 26
  • 42

3 Answers3

1

How about using "validate" method instead of "validates_each" http://paulsturgess.co.uk/articles/33-how-to-write-custom-validation-in-ruby-on-rails

jayandra
  • 296
  • 2
  • 8
0

Looks like you do not have ImageMagick installed on your machine. If you do, type

which identify

and add the path as value to the following paperclip option in environment.rb

Paperclip.options[:command_path] = "/usr/local/bin/" #assuming this folder

Let know how it goes..

dexter
  • 13,365
  • 5
  • 39
  • 56
0

Finally, did with the help of yours input.

validate :icon_dimensions
def icon_dimensions
  unless icon.to_file.nil?
    dimensions = Paperclip::Geometry.from_file(icon.to_file(:original))
    if(dimensions.width > 72 || dimensions.height > 72)
      errors.add(:icon, " dimensions must be less than or equal to 72*72")
    end
  end
end

I hope the validates_each executed before or override the other validations. Not sure :(

ashisrai_
  • 6,438
  • 2
  • 26
  • 42