Rails 5.2 ActiveStorage save and then read Exif data
I think you want to use a variant when displaying the image rather than trying to edit the stored image. To fix the orientation, you could say:
user.avatar.variant(auto_orient: true)
And if you want to do several operations at once (rather than in a pipeline), use combine_options
:
user.avatar.variant(combine_options: {
auto_orient: true,
gravity: 'center',
resize: '23x42', # Using real dimensions of course.
crop: '23x42+0+0'
})
The edited image will be cached so you only do the transformation work on first access. You might want to put your variant
s into view helpers (or maybe even a model concern depending on your needs) so that you can isolate the noise.
You might want to refer to the API docs as well as the guide:
- ActiveStorage::Variant
- ActiveStorage::Variation
Reading from Active Storage Attachment Before Save
Rails 6 changed the moment of uploading the file to storage to during the actual save of the record.
This means that a before_save or validation cannot access the file the regular way.
If you need to access the newly uploaded file you can get a file reference like this:
record.attachment_changes['<attributename>'].attachable
This will be a tempfile of the to-be-attached file.
NOTE: The above is an undocumented internal api and subject to change (https://github.com/rails/rails/pull/37005)
How to retrieve EXIF information of an ActiveStorage image in Rails
If you have a user with avatar maybe ou have a class like that
class User < ApplicationRecord
has_one_attached :avatar
end
To get a exif info you can run this:
user = User.find 1
MiniMagick::Image.open(user.avatar).exif
A gem that you need:
gem "mini_magick"
More about MiniMagick
Rails Api Save MiniMagick Image to Active Storage
Yes, you can. Currently you are trying to save an instance of MiniMagick::Image
to ActiveStorage, and that's why you receive that error. Instead you should attach the :io directly to ActiveStorage.
Using your example, if you wanted to attach mini_img
to a hypothetical User
, that's how you would do it:
User.first.attach io: StringIO.open(mini_img.to_blob), filename: "filename.extension"
In this example I am calling to_blob
on mini_img
, which is an instance of MiniMagick::Image
, and passing it as argument to StringIO#open
. Make sure to include the :filename
option when attaching to ActiveStorage this way.
EXTRA
Since you already have the content_type
when using MiniMagick
you might want to provide it to ActiveStorage directly.
metadata = mini_img.data
User.first.attach io: StringIO.open(mini_img.to_blob), filename: metadata["baseName"], content_type: metadata["mimeType"], identify: false
Related Topics
Dangerousattributeerror in Omniauth Railscast Tutorial: Create Is Defined by Activerecord
Why Is Ruby's Loop Command Slower Than While True
Enforce File Upload Size on Heroku
Secure Erasing of Password from Memory in Ruby
Encoding::Undefinedconversionerror: "\Xc2" from Ascii-8Bit to Utf-8
Omission of Curly Braces for a Hash in an Array
How to Set the Mechanize Page Encoding
Rvm and Gems, Bundle Show and Gem List
Conditional Dependency in Ruby Gemspec
What Does "String Literal in Condition" Mean
How to Sort an Alphanumeric Array in Ruby
Can't Dup Nilclass on Association Methods
Jekyll Plugin Not Work on Github
Rmagick How to Convert Image in Memory