2012-09-24 50 views
2

嗨,我想使用回形針和imagemagick裁剪照片。裁剪照片時出現一些錯誤。以下是錯誤堆棧:使用回形針剪輯照片時面臨的錯誤3

?[32mCommand?[0m :: convert "C:/Users/Anand/AppData/Local/Temp/120120924-3568-tx 
2bxy.jpg[0]" -crop 103x103+0+0 -auto-orient "C:/Users/Anand/AppData/Local/Temp/1 
20120924-3568-tx2bxy20120924-3568-16dij9c" 
?[32mCommand?[0m :: file -b --mime "C:/Users/Anand/AppData/Local/Temp/120120924- 
3568-tx2bxy20120924-3568-16dij9c" 
[paperclip] Error while determining content type: Cocaine::CommandNotFoundError 
?[32mCommand?[0m :: identify -format %wx%h "C:/Users/Anand/AppData/Local/Temp/12 
0120924-3568-tx2bxy.jpg[0]" 
    ?[1m?[36m (0.0ms)?[0m ?[1mrollback transaction?[0m 
Completed 500 Internal Server Error in 145552ms 

NoMethodError (undefined method `exitstatus' for nil:NilClass): 
    app/models/user.rb:14:in `reprocess_photo' 
    app/models/user.rb:14:in `reprocess_photo' 
    app/models/user.rb:14:in `reprocess_photo' 
    app/models/user.rb:14:in `reprocess_photo' 
    app/models/user.rb:14:in `reprocess_photo' 
    app/models/user.rb:14:in `reprocess_photo' 
    app/models/user.rb:14:in `reprocess_photo' 
    app/controllers/users_controller.rb:67:in `block in update' 
    app/controllers/users_controller.rb:66:in `update' 


    Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispa 
tch/middleware/templates/rescues/_trace.erb (10.0ms) 
    Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispa 
tch/middleware/templates/rescues/_request_and_response.erb (1.0ms) 
    Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispa 
tch/middleware/templates/rescues/diagnostics.erb within rescues/layout (51.0ms) 
Exiting 

模型/ user.rb文件:

class User < ActiveRecord::Base 
    attr_accessible :name,:photo,:crop_x,:crop_y,:crop_w,:crop_h 
    has_attached_file :photo,:styles => {:small=>"100x100#",:large=>"500x500>"},:processors => [:cropper] 
    attr_accessor :crop_x,:crop_y,:crop_w,:crop_h 
    after_update :reprocess_photo,:if=>:cropping? 

    def cropping? 
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? 
    end 

    private 

    def reprocess_photo 
    photo.reprocess! 
    end 

end 

我的lib/paperclip_processros/cropper.rb

module Paperclip 
    class Cropper < Thumbnail 
    def transformation_command 
     if crop_command 
     #this generates command : 
     #this is right #convert "C:/Users/Anand/AppData/Local/Temp/1.jpg[0]" -crop 102x102+0+0 -auto-orient "C:/Users/Anand/AppData/Local/Temp/120120924-2336-qbzroo20120924-2336-1jqbiiv.jpg" 
     crop_command + super.first.sub(/ -crop \S+/, '') 
     else 
     super 
     end 
    end 

    def crop_command 
     target = @attachment.instance 
     if target.cropping? 
     " -crop #{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y} " 
     end 
    end 
    end 
end 

我的配置/ ENV的/ dev。 rb

Paperclip.options[:swallow_stderr] = false 
Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.7.9-Q16/" 

當m在船尾更新照片時呃作物,它進入循環。什麼可能是解決方案?

回答

3

我敢肯定你碰到我最初有同樣的問題,由此重新處理!方法是保存ActiveRecord父項,然後觸發after_update回調,導致再次調用reprocess_photo方法。最終結果是一個有些遞歸的循環,直到你的機器耗盡可分配內存纔會停止。

簡單的解決方法是添加一個布爾屬性,稱爲處理,這將允許您確定我們是否已經重新處理圖像。

attr_accessor :processing 

然後改變你的reprocess_photo返回,如果圖像已被處理。

def reprocess_photo 
    # don't crop if the user isn't updating the photo 
    # ...or if the photo is already being processed 
    return unless (cropping? && !processing) 
    self.processing = true 
    photo.reprocess! 
    self.processing = false 
    end 
0

嘗試改變

Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.7.9-Q16/" 

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

好像圖像Magick沒有找到