Ruby on Rails,Paperclip:“identify”命令在cmd中正常工作但在应用程序中不起作用。

3

我已经在我的Windows 7 64位系统上安装了ImageMagick,并且安装了Paperclip Gem。我的用户模型如下:

   class User < ActiveRecord::Base
  # Paperclip
  has_attached_file :photo,
    :styles => {
      :thumb=> "100x100#",
      :small  => "150x150>" }
  end

在paperclip.rb和development.rb中,我有以下内容:
Paperclip.options[:command_path] = 'C:/Program Files/ImageMagick-6.6.7-Q16'

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

我在上传图片时遇到了以下错误:
[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.>  

我能够在命令提示符中使用identify命令,并顺利返回该图像的元数据。请帮忙解决问题,我已经被卡在这个问题上一天了。
3个回答

7

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

def full_path(binary)
  [self.class.path, binary].compact.join("/")
end

full_path函数生成带反斜杠的命令文件名。

"C:\Program Files\ImageMagick-6.7.0-Q16"/identify

这个命令在Windows上会失败,因为cmd shell在命令文件是带反斜杠的长文件名时会抛出错误。
有两种方法可以解决这个问题。

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

Paperclip.options[:command_path] = 'C:/PROGRA~1/IMAGEM~1.0-Q'

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

dir /x "C:\Program Files*"
dir /x "C:\Program Files\ImageMagick-6.7.0-Q16*"

config\initializers\paperclip.rb中进行Monkey patch操作。

我在2.3.11版本上测试过此方法。

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

现在,identify命令已使用正确的路径分隔符生成。
"C:\Program Files\ImageMagick-6.7.0-Q16"\identify

我更倾向于第二种方法,因为command_path更容易配置。


4

我更新了development.rb中的以下内容,然后它开始工作了。

Paperclip.options[:command_path] = 'C:/Progra~1/ImageM~1.8-q'

这是在一台Windows 2008 32位服务器上发生的


0
打开命令提示符并输入 echo %path%,你的imagemagick路径应该会出现在那里。
另外尝试将 :command_path 更改为 C:/Progra~1/ImageM~1

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