Created
March 24, 2012 18:59
-
-
Save robertjwhitney/2186736 to your computer and use it in GitHub Desktop.
How to access the dimensions of an image in the view using carrierwave and mini_magick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Photo < ActiveRecord::Base | |
mount_uploader :file, StillImageUploader | |
def dimensions(version = nil) | |
image ||= MiniMagick::Image.open(file.url(version)) | |
{width: image['width'], height: image['height']} | |
end | |
end | |
# photo.dimensions | |
# => {:width=>545, :height=>300} | |
# | |
# photo.dimensions(:thumbnail) | |
# => {:width=>100, :height=>100} | |
# | |
# photo.dimensions[:width] | |
# => 545 | |
# | |
# photo.dimensions[:height] | |
# => 300 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment