What we're listening to:
Jared

The Album Leaf:
Into The Blue Again
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?

  • http://adamhunter.me/ Adam

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

  • http://adamhunter.me Adam

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

  • 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.

  • 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.

  • Dave Mauldin

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

  • Dave Mauldin

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

  • Jared Moody

    @Dave can you post your test code too?

  • http://yourelevation.com Jared

    @Dave can you post your test code too?

  • http://www.pervasivecode.com/blog/ Jamie Flournoy

    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.

  • http://www.pervasivecode.com/blog/ Jamie Flournoy

    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

    Thanks for posting the tip Jamie!

  • http://yourelevation.com Jared

    Thanks for posting the tip Jamie!

  • Rohan

    Ohh thanks…googling helped me land on this quickly and did the job for me

  • http://www.railsmine.net aji

    many thanks for this

  • http://www.gasgoo.com/auto-products/hvac-parts-347/ zhanglijue

    Safety Buckle

    tuxin_tuxin@yahoo.com

    http://www.gasgoo.com/auto-products/safety-belts-buckle-426/

    At the first level of control in all nuclear reactors, a process of Car Mould
    emission by a number of neutron-rich fission isotopes is an important physical process. These delayed neutrons account for about 0.65% of the total neutrons produced in fission, with the remainder (termed “prompt neutrons”) released immediately upon fission. The fission products which produce delayed neutrons have half lives for their decay by Braking System
    that range from milliseconds to as long as several minutes. Keeping the reactor in the zone of chain-reactivity where delayed neutrons are necessary to achieve a critical mass state, allows time for mechanical devices or human operators to have time to control a auto equipment
    in “real time”; otherwise the time between achievement of car electricity
    and Drivetrain
    as a result of an exponential power surge from the normal nuclear chain reaction, would be too short to allow for intervention. This division manufactures steel wheels for trucks, buses and agricultural car material and complete chassis.