Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave and mini_magick finding widths & height

After a bit of investigation I decided to use Carrierwave and mini_magick on my new rail3 app.

I've set it up and it works perfectly. However I have a one question. I'd like to be able to access the width and height, so I can form the html correctly. However, there is no default data from which to get this information. Because of the way it stores the data I'm I cannot think of any way that I can add it to the database.

Can anyone suggest any tips or ideas? Is it even possible?

like image 843
DJ Forth Avatar asked Dec 15 '10 19:12

DJ Forth


2 Answers

class Attachment
  mount_uploader :file, FileUploader

  def image
    @image ||= MiniMagick::Image.open(file.path)
  end
end

And use it like this:

Attachment.first.image['width'] # => 400
Attachment.first.image['height'] # => 300
like image 156
Pointless One Avatar answered Sep 27 '22 19:09

Pointless One


Just for record, I have used a similar solution, however using files with Mongo GridFS, here it goes:

  def image
    @image ||= MiniMagick::Image.read(Mongo::GridFileSystem.new(Mongoid.database).open(file.path, 'r'))
  end
like image 41
Arthur Neves Avatar answered Sep 27 '22 21:09

Arthur Neves