Getting Paperclip attachment sizes

Nov 5, 2010 14:42 · 155 words · 1 minute read code paperclip rails ruby

Another post from the “so I remember how to do this” series.

I’m using Paperclip to handle file attachments (in this context to store profile images which have been retrieved from Facebook, although that’s not strictly relevant).

Although you can constrain the size of the stored image in the Paperclipped model, like so:

# Paperclip stuff
    has_attached_file :avatar, :styles => {
        :medium => "300x300>",
        :thumb => "100x100>"

this will automatically maintain the aspect ratio of the image, so you’re never quite sure what x and y sizes you’ll end up with.

To find these values doesn’t seem to be quite as straight-forward as it might be - this is how I eventually figured it out. Assuming that you’ve got an instance of the Paperclip-enabled model in @profile, then


will return the width and height of the medium sized. You can substitute any of the size keys, or original for the original values.