Rails paperclip和倒置的图片问题

12
我最近遇到了一个问题,用户上传了一张图片,但是在某个地方,Paperclip把它翻转了。您可以在这里看到相关的图片http://photoramblr.com/photos/36。正如您所见,图片是倒置的;但是将该图像拖到桌面上,它就会正常显示。由于这张图片是在iPhone上拍摄的,我只能认为这与iPhone上的图像定向设置有关。有人遇到过类似的情况吗?或者对如何解决这个问题有什么建议吗?这里的代码非常简单直接,就是Paperclip行话。
class Photo < ActiveRecord::Base
  has_attached_file :image,
    :storage => :s3,
    :s3_credentials => S3_CREDENTIALS,
    :styles => {
      :thumb => "100x100#",
      :small => "138x138>",
      :large => "580x580>",
      :x_large => "1600x1600>"}

更新

嗯,我通过截取图像并上传来解决了这个问题。可能是元数据中的某些信息没有正确传递导致了方向不正确的问题。

4个回答

11

是的,这是我们上周在工作中解决的问题。如果您正在使用ImageMagick / RMagic进行图像处理,则可以使用Image#auto_orient来“根据图像的EXIF方向标记旋转或翻转图像”; 在Paperclip处理器中调用此方法即可。

[编辑]

您可能会对Rails,Paperclip,-auto-orient和调整大小...感兴趣。我还发现CarrierWave使这个过程非常容易:

class ImageUploader < CarrierWave::Uploader::Base
  ... # config here

  process :rotate

  def rotate
    manipulate! do |image|
      image.auto_orient
    end
  end
end

嘿,谢谢你的建议;我一定会实施的。 - Tony Beninate
1
这是对我有效的代码:def rotate manipulate! do |img| img.auto_orient img = yield(img) if block_given? img end end - anu
1
我不得不将其更改为 image.tap(&:auto_orient),否则我会得到 NoMethodError: undefined method destroy!' for true:TrueClass`。 - Besi

5

源文件选项

Paperclip添加了一个source_file_options,允许您传递处理器选项直接应用于源文件,且在生成后续缩略图和样式之前

您可以添加此选项以自动调整源文件的方向,如下所示:

class Photo < ActiveRecord::Base
  has_attached_file :image,
    storage:             :s3,
    s3_credentials:      S3_CREDENTIALS,
    source_file_options: { all:     '-auto-orient' },
    styles:              { thumb:   "100x100#",
                           small:   "138x138>",
                           large:   "580x580>",
                           x_large: "1600x1600>" }

此功能应该可在 gem 的 2.3.16 版本中使用。

更多信息,请参阅 Paperclip 在 Github 存储库上的以下问题:

https://github.com/thoughtbot/paperclip/issues/591

原始样式

设置一个original样式以创建自动定向和大小限制的版本也是个好主意,例如:

original: "5000x5000>"

注意:如果您希望上传的不仅是图像,例如PDF文件,则不保留原始PDF文件而只存储PDF文件第一页的图像将会导致问题。


0

只需像这样在您的样式中添加原始代码:{convert_options:'-auto-orient'}

has_attached_file :image,
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:styles => {original: {convert_options: '-auto-orient'},
  :thumb => "100x100#",
  :small => "138x138>",
  :large => "580x580>",
  :x_large => "1600x1600>"}

0
这是最终对我起作用的解决方案:
process :rotate
def rotate
  manipulate! do |img|
    img.auto_orient
    img = yield(img) if block_given?
    img
  end
end

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