LoginSignup
8
13

More than 5 years have passed since last update.

【Rails】Carrierwaveの使用中に画像のexif情報を取得したいとき

Last updated at Posted at 2016-05-03

Carrierwaveを使用して画像をアップロードしているときにexif情報を取り出したい時が有りました。

exifを取得するgemには、
gem 'exifr' か RMagick
がいいよ見たいなことが書いてある記事がありました。

ので、試してみました。

まずCarrierwaveで作成したUploaderにメソッドを追加しておきます

get_exif_infoの中で作業します。

Carrierwaveの簡単なセッティングはこちら
http://qiita.com/funao/items/cd4c4e33cf43767338e2

app/uploaders/image_uploader.rb
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  ...

  process :get_exif_info

  def get_exif_info
  end 

  ...

end

じゃあ、早速比較して行きます

①exifrの場合

試す場合、gemはインストールしておいてください!

app/uploaders/image_uploader.rb
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  ...

  process :get_exif_info

  def get_exif_info
    exif = EXIFR::JPEG::new(self.file.file)
    binding.pry
  end 

  ...

end

取得して表示すると

$ p exif

{
    :width => 400,
   :height => 390,
     :bits => 8,
 :comments => nil
}

こんな感じでとれます
https://remvee.github.io/exifr/api/index.html

めっちゃ便利です

②RMagickの場合

app/uploaders/image_uploader.rb
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  ...

  process :get_exif_info

  def get_exif_info
       exif = Magick::Image.read(self.file.file).first
    binding.pry
  end 

  ...

end

$ p exif.properties

とか書くと

スクリーンショット 2016-05-03 22.56.45.png

たくさんとれます

めっちゃ便利!すごい!
(ほたるの光がながれてる!お店がしまる!!やばい!!笑)

8
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
13