简体   繁体   English

如何从回形针附加的图像中读取exif数据?

[英]How do I read the exif data from an image being attached by paperclip?

I'm trying to read the exif data from jpeg images that I upload with paperclip using the exifr gem and save focal length, etc to attributes but I can't seem to do it. 我正在尝试从jpeg图像中读取exif数据,我使用exifr gem使用paperclip上传并保存焦距等属性,但我似乎无法做到。 I'm a rails beginner and would be most appreciative if anyone could assist. 我是铁轨初学者,如果有人能帮忙,我会非常感激。

my photo.rb model: 我的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

my form (using SimpleForm): 我的表单(使用SimpleForm):

<%= simple_form_for @photo, :html => {:multipart => true}  do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.file_field :photo, :label => 'Attach photo' %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

show.html.erb with fields I'm trying to show: show.html.erb包含我想要显示的字段:

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

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

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

I'm probably missing something stupid so I apologize if that's so. 我可能错过了一些愚蠢的东西所以我道歉,如果是这样的话。

Try changing the first line in your load_exif method to: 尝试将load_exif方法中的第一行更改为:

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. 如果没有.path ,我不相信它会返回文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM