如何从使用Paperclip附加的图像中读取exif数据?

4

我想读取使用 exifr gem 上传的 JPEG 图像中的 exif 数据,并将焦距等内容保存到属性中,但我似乎无法做到。我是一名 Rails 初学者,如果有人能够帮助我,我将不胜感激。

我的 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

我的表格(使用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中我正在尝试显示的字段:

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

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

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

如果我错过了一些愚蠢的东西,那么我很抱歉。

1个回答

4
尝试将您的load_exif方法中的第一行更改为:
exif = EXIFR::JPEG.new(photo.queued_for_write[:original].path)

没有以.path结尾,我认为它不会返回文件路径。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接