I recently needed to find out the geometry of the image being uploaded via Carrierwave. Images come in many different sizes and shapes. What I want is to have a “best” image and store its actual size along with my image model.
Define a Best Image
First, lets define a “best” version of the image. That’s one that’s not being resized, only converted into JPG.
Fetch Geometry on Upload
Notice the process declarations above: both resize_to_limit and convert are methods of the uploader class. We can therefore add a new get_geometry function and store the geometry of the uploaded image with this version.
Save Geometry
Finally, we would like to store best_width and best_height with the Image model. We use MongoId, so we can fetch the geometry before_save.
I think image geometry should be a built-in function and property of Carrierwave. Maybe this can be improved further and make it into the library?