What we're listening to:
Jared

The Hold Steady:
Heaven Is Whenever
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?

  • aji
    many thanks for this
  • Rohan
    Ohh thanks...googling helped me land on this quickly and did the job for me
  • Jared Moody
    Thanks for posting the tip Jamie!
  • Identify hangs when Paperclip invokes it like this:

    identify -format "%wx%h" ""[0] 2> /dev/null

    note the "" before [0]. Identify sees this:

    identify -format %wx%h [0]

    and never does anything.

    To avoid this make sure you don't pass a null to from_file. Inside the validate method add this condition around the code above:

    def validate
    if self.image && self.image.queued_for_write[:original]
    # ... dimensions = etc etc
    end
    end

    and it will stop hanging.
  • Jared Moody
    @Dave can you post your test code too?
  • Dave Mauldin
    The code can be seen at http://gist.github.com/211365
  • Dave Mauldin
    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.
  • I wish i googled this before i went through the trouble of building it manually. thanks!
blog comments powered by Disqus