2010/08/05

Paperclip を Rails 3 + Windows で動かすニャンコ

一日ハマッタので備忘録


今まで動いていたアプリを 2.3.8 から Rails 3.0.0beta4 にアップグレードしようとしたときのこと。
参考までに、今回の Gemfile
gem 'rmagick', :require => 'RMagick' 
gem "aws-s3", ">= 0.6.2",:require => "aws/s3"
gem "paperclip", ">= 2.3.3", :git => "git://github.com/thoughtbot/paperclip.git"

ところが、Paperclip が動かない。。。。 Image C:/Users/Me/AppData/Local/Temp/stream,4508,0.jpg is not recognized by the 'identify' command. だそうな。 identify (ImageMagick) が見つからないらしいので、チェックする。
$ which identify
/c/Program Files/ImageMagick-6.5.6-Q8/identify
こちらに対処法があったので、その通りに config/development.rb に追加
Paperclip.options[:command_path] = "/c/Program Files/ImageMagick-6.5.6-Q8/"
ところが undefined method `exitstatus' for nil:NilClass と。。。orz
/c/Program Files/ImageMagick-6.5.6-Q8/identify -format %wx%h "C:/Users/Me/AppData/Local/Temp/stream,5880,0.jpg[0]"
をアクセスしようとして、エラっている模様。じゃあ、コマンドプロムプトでマニュアル実行してみる。
> "/c/Program Files/ImageMagick-6.5.6-Q8/identify" -format %wx%h "C:/Users/Me/AppData/Local/Temp/stream,5880,0.jpg[0]"
> 指定されたパスが見つかりません。
なるほど。でも windows 風だとOK。
>"c:\Program Files\ImageMagick-6.5.6-Q8\identify" -format %wx%h "C:/Users/Me/Ap
pData/Local/Temp/stream,5880,0.jpg"
> 500x499 
なので、config/development.rb を次のように編集
Paperclip.options[:command_path] = "c:\\Program Files\\ImageMagick-6.5.6-Q8\\"
なおった!!ばんざ~い!

あと、念のためにこちらのスレッドにある変更も・・・
bundler の gem ディレクトリの各ファイルを編集(僕の場合は C:\Users\Me\.bundle\ruby\1.8\bundler\gems\paperclip-XXX-master\lib\paperclip)
因みに、インストールに失敗した別のバージョンの Paperclip gem が残っていてエラっていたこともあるので、綺麗にするように。 command_line.rb
    def shell_quote(string)
      return "" if string.nil? or string.blank?
      # string.split("'").map{|m| "'#{m}'" }.join("\\'")
      string.split("'").map{|m| "\"#{m}\"" }.join("\\'") 
    end
thumbnail.rb
    def transformation_command
      scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
      trans = []
     # trans << "-resize" << "'#{scale}'" unless scale.nil? || scale.empty?
     # trans << "-crop" << "'#{crop}'" << "+repage" if crop
       trans << "-resize" << "\"#{scale}\"" unless scale.nil? || scale.empty?
       trans << "-crop" << "\"#{crop}\"" << "+repage" if crop
      trans
    end

2010/9/2 Hosoku


It turns out adding this would be enough. Create a file initializers/paperclip.rb with the following contents.

if RUBY_PLATFORM == 'i386-mingw32'
  module Paperclip
    def self.quote_command_options(*options)
      options.map do |option|
        option.split("\"").map{|m| "\"#{m}\"" }.join("\\\"")
      end
    end
  end
end

0 件のコメント: