0

所以我使用的是 windows 7、Rails 3、最新的回形针 gem 和 ImageMagick-6.7.7-Q16(在 cmd 中测试),我的 PATH 环境已更新。

模型

class Image < ActiveRecord::Base
  attr_accessible :description, :user_id, :file
  has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :storage => :filesystem

  belongs_to :user

  #validations
  validates_attachment_presence :file
  validates_attachment_size :file, :less_than => 4.megabytes
  validates_attachment_content_type :file, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
end

形式

<%= form_for(@image, :html => { :multipart => true }) do |image| %>

    <div class="control-group">
      <%= image.label :description, "Description", :class => 'control-label' %>
      <div class="controls">
        <div class="input-prepend">
          <%= image.text_field :description %>
        </div>
      </div>
    </div>

    <div class="control-group">
      <%= image.label :file, "Image", :class => 'control-label' %>
      <div class="controls">
        <div class="input-prepend">
          <%= image.file_field :file %>
        </div>
      </div>
    </div>

    <%= image.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
      <%= image.submit "Upload Image", :class => 'btn btn-primary btn-medium' %>
    </div>
<% end %>

我的 paperclip.rb 在初始化程序中

require "paperclip"
Paperclip.options[:command_path] = 'C:\Program Files (x86)\ImageMagick-6.7.7-Q16'
Paperclip.options[:swallow_stderr] = false
Paperclip.options[:whiny_thumbnails] = true

一切正常,无需裁剪 (:styles => { :medium => "300x300>", :thumb => "100x100>" })。但是当我想制作缩略图时,回形针会抛出这个错误

Command :: identify -format %wx%h "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]"
Command :: convert "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]" -resize "300x300>" "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw520120519-7696-1p8rcsr"
[paperclip] An error was received while processing: #<Paperclip::Error: There was an error processing the thumbnail for DSCN630520120519-7696-18l3nw5>
Command :: identify -format %wx%h "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]"
Command :: convert "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]" -resize "100x100>" "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw520120519-7696-tx3bmo"
[paperclip] An error was received while processing: #<Paperclip::Error: There was an error processing the thumbnail for DSCN630520120519-7696-18l3nw5>

我发现上传的图像甚至不在临时文件夹中,所以这些创建的图像只有 0 个字节。如果回形针或图像上传有问题,我完全被困在这里。任何帮助将不胜感激

4

1 回答 1

1

这可能意味着您没有正确指向 IM。要么您还没有安装它,要么只是没有按预期找到它。改用双反斜杠:'C:\\Program Files (x86)\\ImageMagick-6.7.7-Q16'. (为了避免空格,您还可以使用 8.3 文件名系统来标识带有正斜杠的路径。)

如果您使用的是最新版本的 Paperclip (3.1.4),在 Windows 上,我建议您从 GnuWin32 安装 File 包。请看这篇文章:https ://github.com/thoughtbot/paperclip/issues/926 。

最后,为了支持文件名中的非英文字符,您可能需要对 Cocaine 进行猴子补丁。我在初始化程序之间放置了一个 cocaine_path.rb。请注意,“iso-8859-1”可能不适合您。

cocaine_pacth.rb:

if RUBY_PLATFORM == "i386-mingw32"
  module Cocaine
    class CommandLine
      def run
        output = ''
        begin
          with_modified_path do
            @logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
            ec = Encoding::Converter.new("utf-8", "iso-8859-1")
            output = self.class.send(:'`', ec.convert(command.encode('UTF-8')))
          end
        rescue Errno::ENOENT
          raise Cocaine::CommandNotFoundError
        end
        if $?.exitstatus == 127
          raise Cocaine::CommandNotFoundError
        end
        unless @expected_outcodes.include?($?.exitstatus)
          raise Cocaine::ExitStatusError, "Command '#{command}' returned #{$?.exitstatus}. Expected #{@expected_outcodes.join(", ")}"
        end
        output
      end
    end
  end
end

这涵盖了我最近几天在 Windows 上使用 Paperclip 时遇到的所有问题。我希望它有所帮助。

于 2012-08-02T13:08:13.527 回答