RailsCasts Pro episodes are now free!

Learn more or hide this

Michael Chaney's Profile

GitHub User: mdchaney

Site: http://www.michaelchaney.com/

Comments by Michael Chaney

Avatar

Please see this project on github:

https://github.com/mdchaney/CarrierWave-ImageSize

It does what you need pretty easily. Just stick it in your lib directory and then use it from your uploader by first loading the file:

require 'carrierwave/image_size'

and then including it in the class:

include CarrierWave::ImageSize

You need to add another field to your table to keep the extra info. If your picture is "avatar", you add "avatar_information" as a wide varchar field. It will then store size information for your image and various versions, as well as a content type. You then use "@user.avatar.image_width", "@user.avatar.thumb.image_height", etc. to get the size. It adds "image_width", "image_height", and "content_type" methods to all versions. It also works with nested versions.

It does require ImageMagick, specifically the "identify" command.

It would be a good upgrade to use MiniMagick or RMagick to get the size. It would also be nice for this to be turned into a gem. I just don't have time, but I am using this code on multiple web sites.

Hope that helps.