Ruby on Rails,Paperclip:"识别"命令在cmd中工作但不在app中工作

Joh*_*ohn 3 imagemagick paperclip ruby-on-rails-3

我在Windows 7 64bit上安装了ImageMagick,我有Paperclip Gem.我的用户模型如下所示:

   class User < ActiveRecord::Base
  # Paperclip
  has_attached_file :photo,
    :styles => {
      :thumb=> "100x100#",
      :small  => "150x150>" }
  end
Run Code Online (Sandbox Code Playgroud)

在paperclip.rb和development.rb中,我有:

Paperclip.options[:command_path] = 'C:/Program Files/ImageMagick-6.6.7-Q16'
Run Code Online (Sandbox Code Playgroud)

我的_form看起来像这样:

    <%= form_for(@user, :html => { :multipart => true } )  do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :username %><br />
    <%= f.text_field :username %>
  </div>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :crypted_password %><br />
    <%= f.text_field :crypted_password %>
  </div>
  <div class="field">
    <%= f.label :password_salt %><br />
    <%= f.text_field :password_salt %>
  </div>
 <%= f.file_field :photo%>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

enter code here
Run Code Online (Sandbox Code Playgroud)

上传图片时出现以下错误:

[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: C:/Users/John/AppData/Local/Temp/stream20110212-6576-1us1cdl.png is not recognized by the 'identify' command.>  
Run Code Online (Sandbox Code Playgroud)

我能够在我的cmd中使用该图像上的识别,并且它可以毫无问题地返回有关图像的元数据.

如果可以的话请帮忙.我已经被困在这一个问题上超过一天了.

Har*_*tty 7

这是由于lib/paperclip/command_line.rb 文件中的Paperclip gem中存在错误.

def full_path(binary)
  [self.class.path, binary].compact.join("/")
end
Run Code Online (Sandbox Code Playgroud)

full_path函数使用反斜杠生成命令文件名.

"C:\Program Files\ImageMagick-6.7.0-Q16"/identify
Run Code Online (Sandbox Code Playgroud)

此命令在Windows上失败,因为cmd当命令文件是带有反斜杠的长文件名时,shell会引发错误.

有两种方法可以解决问题.

使用短文件名作为命令路径.

Paperclip.options[:command_path] = 'C:/PROGRA~1/IMAGEM~1.0-Q'
Run Code Online (Sandbox Code Playgroud)

注意:您可以按如下方式获取短文件名:

dir /x "C:\Program Files*"
dir /x "C:\Program Files\ImageMagick-6.7.0-Q16*"
Run Code Online (Sandbox Code Playgroud)

猴子修补了Paperclip宝石config\initializers\paperclip.rb.

我在2.3.11测试了这个.

class Paperclip::CommandLine
  def full_path(binary)
    [self.class.path, binary].compact.join(File::ALT_SEPARATOR||File::SEPARATOR)
  end
end
Run Code Online (Sandbox Code Playgroud)

现在,identify使用正确的路径分隔符生成命令.

"C:\Program Files\ImageMagick-6.7.0-Q16"\identify
Run Code Online (Sandbox Code Playgroud)

我更喜欢第二种方法,因为command_path它更容易配置.