回形针可以从S3铲斗中读取照片几何图形吗?

Tri*_*rip 12 ruby ruby-on-rails imagemagick amazon-s3 paperclip

我想从S3容器中读取照片的几何图形.

当它在我的本地时,这工作:

def photo_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file photo.path(style)
end
Run Code Online (Sandbox Code Playgroud)

但是当我将模型切换到S3时它似乎不起作用..任何建议?

更重要的是,我正在尝试编写一些代码,允许我从S3中检索照片,允许用户裁剪它们,然后将它们重新上传回仍由回形针分配的S3.

编辑:

这是返回的错误:

Paperclip::NotIdentifiedByImageMagickError: photos/199/orig/greatReads.png is not recognized by the 'identify' command.
from /Users/daniellevine/Sites/hq_channel/vendor/gems/thoughtbot-paperclip-2.3.1/lib/paperclip/geometry.rb:24:in `from_file'
from /Users/daniellevine/Sites/hq_channel/app/models/photo.rb:68:in `photo_geometry'
from (irb):1
Run Code Online (Sandbox Code Playgroud)

Tri*_*rip 14

如果您使用S3作为存储机制,您不能使用上面的几何方法(它假定本地文件).Paperclip可以通过以下方式从S3文件转换为本地TempFile Paperclip::Geometry.from_file:

这是我更新的代码:

def photo_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file(photo.to_file(style))
end
Run Code Online (Sandbox Code Playgroud)

  • 在paperclip 3.0.1中删除了#to_file.在该版本和更高版本中,使用`Paperclip :: Geometry.from_file(Paperclip.io_adapters.for(photo.styles [style])) (2认同)
  • @IsaacBetesh这对我不起作用.我收到以下错误:`Paperclip :: AbstractAdapter#path委托给@ tempfile.path,但@tempfile是nil:Paperclip :: NilAdapter`.仅供参考,我正在使用带有雾宝石的s3. (2认同)

Pav*_*ara 10

这适用于s3和本地

def photo_geometry(style = :original)
  @geometry ||= {}
  photo_path = (photo.options[:storage] == :s3) ? photo.url(style) : photo.path(style)
  @geometry[style] ||= Paperclip::Geometry.from_file(photo_path)
end
Run Code Online (Sandbox Code Playgroud)