Rails Paperclip并不了解与ImageMagick相同的转换选项

时间:2014-11-20 05:29:59

标签: ruby-on-rails imagemagick paperclip

当高于600Kb而不是.gif:

时,尝试将最大宽度设置为1280
has_attached_file :main_image,
  :styles => {:original => "" },
  :convert_options => {
    :original => lambda { |instance| (instance.main_image_file_name.index(/\.gif/,-4).nil? && instance.main_image.size > 600000) ? "-resize 1280>" : "" }
  },
  :path => ":rails_root/public/system/:class/:attachment/:id_:basename.:extension",
  :url => "/system/:class/:attachment/:id_:basename.:extension"

在我的日志中获取此信息:

Command :: convert '/var/folders/hn/2q5wpxh52nn_3nk9wwskr48w0000gn/T/5827912cb359afd550a72bf4f54b109020141119-76993-1i6nij6.png[0]' -auto-orient -resize 1280> '/var/folders/hn/2q5wpxh52nn_3nk9wwskr48w0000gn/T/5827912cb359afd550a72bf4f54b109020141119-76993-1i6nij620141119-76993-q0m833'
sh: 1280: Bad file descriptor
[paperclip] An error was received while processing: #<Paperclip::Error: There was an error processing the thumbnail for 5827912cb359afd550a72bf4f54b109020141119-76993-1i6nij6>

1 个答案:

答案 0 :(得分:0)

这里有两个问题:

  1. 带有Rails的Paperclip通常会要求您在\\<字符前添加>,如1280x\\>
  2. 此外,Paperclip总是需要那些x,即使只做宽度(1280x\\>而不是1280\\>)。
  3. 这是我在600Kb以上而不是.gif时将最大宽度设置为1280的完整解决方案:

    has_attached_file :main_image,
      :styles => {:original => "" },
      :convert_options => {
        :original => lambda { |instance| (instance.main_image_file_name.index(/\.gif/,-4).nil? && instance.main_image.size > 600000) ? "-resize 1280x\\>" : "" }
      },
      :path => ":rails_root/public/system/:class/:attachment/:id_:basename.:extension",
      :url => "/system/:class/:attachment/:id_:basename.:extension"