How to retrieve EXIF information of an image in Rails

后端 未结 4 1051
囚心锁ツ
囚心锁ツ 2021-02-12 12:23

I am using Rails paperclip for displaying the images in my page. I want to know how to retrieve EXIF information of an image(like dimensions, camera model,height,width., etc).Ca

4条回答
  •  遥遥无期
    2021-02-12 12:49

    There are 3 gems to do this:

    1. mini_exiftool: ExifTool command-line wrapper
    2. exifr: Pure Ruby
    3. exif: C Extension (by me)

    If you want to write or edit EXIF tag, you should choose mini_exiftool, it's more powerful but very slow, as the benchmark shown below, exif is 8 times faster than exifr, and 1200 times than that of mini_exiftool.

    benchmark:

    require 'benchmark'
    require 'mini_exiftool'
    require 'exifr'
    require 'exif'
    
    N = 50
    FILE_PATH = File.expand_path('../../spec/sample.jpg', __FILE__)
    Benchmark.bmbm do |x|
      x.report 'mini_exiftool' do
        N.times{ MiniExiftool.new(FILE_PATH).image_width }
      end
      x.report 'exifr' do
        N.times{ EXIFR::JPEG.new(FILE_PATH).width }
      end
      x.report 'exif' do
        N.times{ Exif::Data.new(FILE_PATH).image_width }
      end
    end
    

    output:

    Rehearsal -------------------------------------------------
    mini_exiftool   0.150000   0.050000  12.390000 ( 12.546417)
    exifr           0.090000   0.000000   0.090000 (  0.091090)
    exif            0.010000   0.000000   0.010000 (  0.010343)
    --------------------------------------- total: 12.490000sec
                        user     system      total        real
    mini_exiftool   0.150000   0.050000  12.400000 ( 12.540122)
    exifr           0.080000   0.000000   0.080000 (  0.083251)
    exif            0.010000   0.000000   0.010000 (  0.009855)
    

    mini_exiftool is a bit overkill to only retrieve data. So in your case, I think you should use exifr in JRuby, or give exif a try in MRI.

提交回复
热议问题