Search code examples
ruby-on-railsruby-on-rails-4minimagickrefile

Get image dimensions using Refile


Using the Refile gem to handle file uploading in Rails, what is the best way to determine image height and width during / after it has been uploaded? There is no built in support for this AFAIK, and I can't figure out how to do it using MiniMagick.


Solution

  • @russellb's comment almost got me there, but wasn't quite correct. If you have a Refile::File called @file, you need to:

    fileIO = @file.to_io.to_io
    mm = MiniMagick::Image.open(fileIO)
    mm.width # image width
    mm.height # image height
    

    Yes, that's two calls to #to_io >...< The first to_io gives you a Tempfile, which isn't what MiniMagick wants. Hope this helps someone!

    -- update --

    Additional wrinkle: this will fail if the file is very small (<~20kb, from: ruby-forum.com/topic/106583) because you won't get a tempfile from to_io, but a StringIO. You need to fork your code if you get a StringIO and do:

    mm = MiniMagick::Image.read(fileio.read)
    

    So my full code is now:

    # usually this is a Tempfile; but if the image is small, it will be 
    # a StringIO instead >:[
    fileio = file.to_io
    
    if fileio.is_a?(StringIO)
      mm = MiniMagick::Image.read(fileio.read)
    else
      file = fileio.to_io
      mm = MiniMagick::Image.open(file)
    end