119 Labs LLC

Ideas on Technology

Better Image Dimensions With Paperclip

A while ago I had the need to add image dimensions (height & width) to my paperclip attachments. After a lot of research I came across this: https://gist.github.com/343678 It did the job OK, but had a few problems:
  • I hated adding the my_style attribute
  • Dimension calculation was flaky and often produced ghost failures
So instead I rewrote the caching part to listen for paperclip processing events and use introspection to determine attachments and styles. I also took a suggestion (thanks Win) and moved it into an initializer.  Here’s the updated file: https://gist.github.com/827827 In order to get this working you need to add a couple lines to the model that currently has the Paperclip::Attachment https://gist.github.com/827533 Seems to work much more reliably now. Also, notice the lack of my style attributes. One more note, if you’re adding dimensions to your model where they weren’t before, make sure to add a <attachment_name>_dimensions column to your model (should be type :text on sql)