What we're listening to:
Jared

Thrice:
Beggars
Jeff

Paper Route:
Absence

I’m liking paperclip, in a recent project I chose it specifically because it buffers writes on uploads, which should help keep memory usage down.

I needed to validate that an uploaded image was at least a certian size, and it was a little tricky, but here’s what I came up with:

 has_attached_file :image,
                   :styles => { :original => ["1000x600>", :jpg]},
                   :whiny_thumbnails => true

 def validate
    dimensions = Paperclip::Geometry.from_file(self.image.queued_for_write[:original])
    self.errors.add(:image, "Please upload a file at least 700 pixels wide") if dimensions.width < 700
    self.errors.add(:image, "Please upload a file at least 200 pixels tall") if dimensions.height < 200
  end

like it? hate it? have something better?

4 Responses to “validate image dimensions with paperclip”

  1. I wish i googled this before i went through the trouble of building it manually. thanks!

  2. So I’m trying to do something similar but running into an issue while running tests. The method you’ve posted causes tests to hang in Paperclip::Geometry. I even tried just doing a basic piece of code that just gets dimensions after a save.

    Also causes tests to hang. Can’t find any information anywhere on why this is happening and was hoping if you’d experienced it, you’d worked around it.

  3. The code can be seen at http://gist.github.com/211365

  4. @Dave can you post your test code too?

Leave a Reply