RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Rails ActiveStorage is automatically storing width/height of image

When reading ActiveStorage Overview Guild chances are you missed section on #analyze which claims:

 New blobs are automatically and asynchronously analyzed via analyze_later when they're attached for the first time.

That means Given model like this:

class Image < ApplicationRecord
  has_one_attached :file
end

…you can access your image dimensions with:

image.file.metadata
#=> {"identified"=>true, "width"=>2448, "height"=>3264, "analyzed"=>true}

image.file.metadata['width']
image.file.metadata['height']

More info in this article

https://blog.eq8.eu/til/image-width-and-height-in-rails-activestorage.html

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in