开发者

Ruby on Rails, Paperclip: "identify" command working in cmd but not in app

I've installed ImageMagick on my Windows 7 64bit and I have the Paperclip Gem. My User model looks like this:

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

In paperclip.rb and development.rb I have:

Paperclip.options[:command_path] = 'C:/Program Files/ImageMagick-6.6.7-Q16'

My _form looks like this:

    <%= 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.sub开发者_StackOverflow中文版mit %>
  </div>

<% end %>

enter code here

I'm getting the following error when uploading an image:

[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.>  

I'm able to use identify in my cmd on that image and it returns metadata about the image without problems.

Please help if you can. I've been stuck on this one issue for over a day now.


This is due to a bug in the Paperclip gem in lib/paperclip/command_line.rb file.

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

The full_path function generates command file name with a backslash.

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

This command fails on Windows as the cmd shell throws an error when the command file is a long file name with a back slash.

There are two ways to fix the issue.

Use the short file name as the command path.

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

Note: You can get the short file name as follows:

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

Monkey patch the Paperclip gem in config\initializers\paperclip.rb.

I tested this on 2.3.11.

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

Now, the identify command is generated with the correct path seperator.

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

I prefer the second approach as command_path is easier to configure.


Updated the following in development.rb and it started working

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

This was on a Windows 2008 32 Bit Server


Open a command prompt and type echo %path% your imagemagick path should appear there.

Also try changing the :command_path to C:/Progra~1/ImageM~1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜