2011-06-15 60 views
2

我想允許在我的應用上裁剪。然而,當我運行這個方法:ImageMagick和回形針沒有找到我的文件在S3(Jcrop)上的位置

def avatar_geometry(style = :original) 
    @geometry ||= {} 
    @geometry[style] ||= Paperclip::Geometry.from_file avatar.path(style) 
    end 


identify: unable to open image `/original/4/nutra.jpg': No such file or directory @ error/blob.c/OpenBlob/2587. 
Paperclip::NotIdentifiedByImageMagickError: /original/4/nutra.jpg is not recognized by the 'identify' command. 
    from /Users/skline/.rvm/gems/[email protected]/gems/paperclip-cloudfiles-2.3.10.1/lib/paperclip/geometry.rb:26:in `from_file' 
    from /Users/skline/NutraNation1/app/models/user.rb:111:in `avatar_geometry' 
    from (irb):2 
    from /Users/skline/.rvm/gems/[email protected]/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start' 
    from /Users/skline/.rvm/gems/[email protected]/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start' 
    from /Users/skline/.rvm/gems/[email protected]/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>' 

在回形針段出錯的代碼如下所示:

def self.from_file file 
     file = file.path if file.respond_to? "path" 
     geometry = begin 
        Paperclip.run("identify", "-format %wx%h :file", :file => "#{file}[0]") 
       rescue Cocaine::ExitStatusError 
        "" 
       rescue Cocaine::CommandNotFoundError => e 
        raise Paperclip::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.") 
       end 
     parse(geometry) || 
     raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command.")) 
    end 

我的化身模型是這樣的:

AVATAR_SW = 55 
    AVATAR_SH = 55 
    AVATAR_NW = 240 
    AVATAR_NH = 240 

    has_attached_file :avatar, 
     :styles => { :normal => ["#{AVATAR_NW}x#{AVATAR_NH}>", :jpg], 
        :small => ["#{AVATAR_SW}x#{AVATAR_SH}#", :jpg] }, :storage => :s3, 
     :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", 
     :path => "/:style/:id/:filename" 

而且我給Paperclip在我的development.rb中找到ImageMagick的路線是這樣的:Paperclip.options[:command_path] = "/usr/local/bin/"我已經證實這確實是ImageMagick的家園。

有趣的是,我可以上傳圖像沒有任何問題。簡單地說,當我試圖運行這個方法來裁剪它們時,我遇到了問題。有什麼建議麼。使用回形針(2.3.11)

+0

請,請參閱本http://stackoverflow.com/a/12771707/1472432問題可能是在使用可卡因0.4。嘗試降級到可卡因0.3.2。回形針使用可卡因,但新的寶石版本使用錯誤的識別命令格式。 – 2012-10-08 16:51:01

回答

4

這適用於S3和當地

def photo_geometry(style = :original) 
    @geometry ||= {} 
    path = (avatar.options[:storage]==:s3) ? avatar.url(style) : avatar.path(style) 
    @geometry[style] ||= Paperclip::Geometry.from_file(path) 
end 
+0

非常感謝!我花了幾個小時試圖弄清楚如何使這個工作,這段代碼解決了它的第一次嘗試。 – jasonmklug 2011-07-07 17:20:27

+0

@pkumar - 非常感謝!我瘋了試圖解決這個問題 - 仍然不明白爲什麼如果我的:URL和:路徑是相同的附件哈希我需要使用一個幫手在另一個... – 2012-02-15 09:26:49

+0

謝謝@Pavan,這很好。你怎麼知道s3需要完整的url而不是路徑?這是你知道的,還是你以某種方式診斷它? – umezo 2012-11-26 23:24:10