How to retrieve image dimensions? #790

Closed
agis- opened this Issue Mar 26, 2012 · 9 comments

5 participants

@agis-

Is it possible somehow to retrieve the dimensions of a thumbnail generated by Paperclip?

For example it would be useful to have something like this-> @product.image.width; @product.image.height.

@airblade

I don't know about within Paperclip, but outside Paperclip you could try Dimensions.

@uberllama
before_save :extract_dimensions

  # @note Do this after resize operations to account for auto-orientation.
  def extract_dimensions
    tempfile = upload.queued_for_write[:original]
    unless tempfile.nil?
      geometry = Paperclip::Geometry.from_file(tempfile)
      self.dimensions = [geometry.width.to_i, geometry.height.to_i]
    end
  end
@sikachu
thoughtbot, inc. member

You'll be able to do @product.image.geometry.width and @product.image.geometry.height in Paperclip 3.0.1.

@sikachu sikachu closed this Mar 30, 2012
@uberllama

Don't these calls download the file in order to run the geometry calculations? If so, this is expensive and impractical, which is why I personally do it once on a before_save.

@sikachu
thoughtbot, inc. member

Yep, you can. I'd love to take a pull request for a magic attachment_width/attachment_height attribute too, but this is a cheap (easy to implement) solution when your attachment is already saved.

@uberllama

I wouldn't suggest anyone who's using s3 use those methods.

@sikachu
thoughtbot, inc. member

Yeah, and there's no way to get that attachment metadata without fetching the file down anyway after you've saved it. :)

That code is broken anyway, so I'm reverting it.

@sikachu sikachu reopened this Mar 30, 2012
@phene

Checkout #835

@sikachu
thoughtbot, inc. member

I think i am going to merge #835 into 3.1, so closing this.

@sikachu sikachu closed this Apr 25, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment