Ruby on rails 如何从用回形针粘贴的图像中读取exif数据?

Ruby on rails 如何从用回形针粘贴的图像中读取exif数据?,ruby-on-rails,ruby,paperclip,exif,Ruby On Rails,Ruby,Paperclip,Exif,我正在尝试从我用回形针上传的jpeg图像中读取exif数据,使用exifr gem并将焦距等保存到属性,但我似乎做不到。我是一名rails初学者,如果有人能帮助我,我将不胜感激 my photo.rb型号: class Photo < ActiveRecord::Base attr_accessible :date, :exif, :name, :photo has_attached_file :photo, :styles => { :small => "200x200

我正在尝试从我用回形针上传的jpeg图像中读取exif数据,使用exifr gem并将焦距等保存到属性,但我似乎做不到。我是一名rails初学者,如果有人能帮助我,我将不胜感激

my photo.rb型号:

class Photo < ActiveRecord::Base

attr_accessible :date, :exif, :name, :photo
has_attached_file   :photo, :styles => { :small => "200x200#" , :medium => "1280x720#" },
:url  => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"

after_photo_post_process :load_exif

validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg']



def load_exif
  exif = EXIFR::JPEG.new(photo.queued_for_write[:original])
  return if exif.nil? or not exif.exif?
  self.exposure = exif.exposure_time.to_s
  self.f_stop = exif.f_number.to_f.to_s
  self.focal_length = exif.focal_length.to_f.round.to_s
  self.iso = exif.iso_speed_ratings
  self.date = exif.date_time.to_date
  rescue
    false
  end
end
class-Photo{:small=>“200x200”,:medium=>“1280x720”},
:url=>“/assets/photos/:id/:style/:basename.:extension”,
:path=>“:rails\u root/public/assets/photos/:id/:style/:basename.:extension”
照片后处理:加载exif
验证附件是否存在:照片
验证附件内容类型:照片、内容类型=>['image/jpeg']
def load_exif
exif=EXIFR::JPEG.new(照片。排队等待写入[:原始])
如果exif.nil返回?或者不是exif.exif?
self.exposure=exif.exposure\u time.to\u s
self.f\u stop=exif.f\u number.to\u f.to\u s
self.focal_length=exif.focal_length.to_f.round.to_s
self.iso=exif.iso\u速度\u额定值
self.date=exif.date\u time.to\u date
营救
假的
结束
结束
我的表单(使用SimpleForm):

{:multipart=>true}do | f |%>
'附加照片'%>
show.html.erb以及我试图显示的字段:

<p>
  <b>Name:</b>
  <%= @photo.name %>
</p>

<p>
  <b>Date:</b>
  <%= @photo.date %>
</p>

<p>
  <b>Exposure:</b>
  <%= @photo.exposure %>
</p>

姓名:

日期:

暴露:


我可能遗漏了一些愚蠢的东西,如果是这样,我向您道歉。

尝试将
加载\u exif
方法中的第一行更改为:

exif = EXIFR::JPEG.new(photo.queued_for_write[:original].path)
如果最后没有
.path
,我相信它不会返回文件路径

exif = EXIFR::JPEG.new(photo.queued_for_write[:original].path)