How to retrieve EXIF information of an image in Rails
Did you give exifr gem a try? From the documentation
EXIFR::JPEG.new('IMG_6841.JPG').width # => 2272
EXIFR::JPEG.new('IMG_6841.JPG').height # => 1704
EXIFR::JPEG.new('IMG_6841.JPG').exif? # => true
EXIFR::JPEG.new('IMG_6841.JPG').model # => "Canon PowerShot G3"
EXIFR::JPEG.new('IMG_6841.JPG').date_time # => Fri Feb 09 16:48:54 +0100 2007
EXIFR::JPEG.new('IMG_6841.JPG').exposure_time.to_s # => "1/15"
EXIFR::JPEG.new('IMG_6841.JPG').f_number.to_f # => 2.0
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
How do I read the exif data from an image being attached by paperclip?
Try changing the first line in your load_exif
method to:
exif = EXIFR::JPEG.new(photo.queued_for_write[:original].path)
Without .path
on the end, I don't believe it's returning the file path.
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
Rails ActiveStorage strip image EXIF data
based on iGian's comment I ended up with this code:
before_save :strip_exif_data
private
def strip_exif_data
return unless image.attached?
filename = image.filename.to_s
attachment_path = "#{Dir.tmpdir}/#{image.filename}"
File.open(attachment_path, 'wb') do |file|
file.write(image.download)
file.close
end
mm_image = MiniMagick::Image.open(attachment_path)
mm_image.strip
mm_image.write attachment_path
image.attach(io: File.open(attachment_path), filename: filename)
end
Getting GPS metadata from a picture with Paperclip
What you need is being able to extract EXIF information from the picture.
There is a gem that aims to do just that: exifr
You can install it automatically with the following bundler line:
gem 'exifr'
EDIT:
I have personally switched to this fork of the same gem, which contains GPS helpers:
gem 'exifr', :git => 'git://github.com/picuous/exifr.git'
Use example:
EXIFR::JPEG.new('IMG_6841.JPG').gps? # => true
EXIFR::JPEG.new('IMG_6841.JPG').gps # => [37.294112,-122.789422]
EXIFR::JPEG.new('IMG_6841.JPG').gps_lat # => 37.294112
EXIFR::JPEG.new('IMG_6841.JPG').gps_lng # =>-122.789422
How to get EXIF keywords using mini_magick in a Rails app?
EXIF metadata is created by camera, therefore it contains only technical related stuff. What you actually want to access is IPTC and XMP.
Imagemagick, which is behind mini_magick, allows to read IPTC, e.g. image["%[IPTC:2:25]"]
for keywords (update: be aware of perfomance issues, see comments).
As for XMP, I don't know an easy way to do this. You can try to run
`identify -verbose #{your_filename}`
and then grub lines that include xmp
.
Related Topics
Ruby Imap "Changes" Since Last Check
How to Make a Method Available to Both My Controller and Model in Rails
Validate That a Value Is in a Certain Range, E.G. 1 <= Val <=2
Static Site Generator Based Upon Directories & Files
Ruby Syntax: Break Out from 'Each.. Do..' Block
Heroku Rails Rake Task to Sync Production & Local Db
How to Do a Before_Action in Ruby (Like in Rails)
How to Calculate Next, Previous Business Day in Rails
Ruby on Rails - Rack-Cors Multiple Origins with Different Resources
Why Is Foreman Gem Ignoring the Port Environment Variable
Ruby (With Rails) Convert a String of Time into Seconds
Ruby No Implicit Conversion of Fixnum into String (Typeerror)
Application.CSS Not Being Served as an Asset
Run Selenium with Chrome Driver on Heroku: 'Cannot Find Chrome Binary'
Hash Remove All Except Specific Keys
Ruby on Rails Triggers Update on Serialized Attribute Every Time