4

我有一個照片共享應用程序,允許用戶拖放圖像,然後在延遲作業中處理圖像並顯示在圖庫中。我在解決problem with orientation on iPhone pictures時遇到了一些問題。我有以下代碼:使用延遲作業時自動定位圖像

初始化/ auto_orient.rb

module Paperclip 
    class AutoOrient < Paperclip::Processor 
    def initialize(file, options = {}, *args) 
     @file = file 
    end 

    def make(*args) 
     dst = Tempfile.new([@basename, @format].compact.join(".")) 
     dst.binmode 

     Paperclip.run('convert',"#{File.expand_path(@file.path)} -auto-orient #{File.expand_path(dst.path)}") 

     return dst 
    end 
    end 
end 

型號/ picture.rb

class Picture < ActiveRecord::Base 

    belongs_to :gallery 

    before_create :generate_slug 
    after_create :send_to_delayed_job 

    validates :slug, :uniqueness => true 

    scope :processing, where(:processing => true) 

    attr_accessible :image 

    has_attached_file :image, 
    :styles => { 
    :huge => "2048x1536>", 
    :small => "800x600>", 
    :thumb => "320x240>" 
    }, 
    :processors => [:auto_orient, :thumbnail] 

    before_post_process :continue_processing 

    ... 

    def process 
    self.image.reprocess! 
    self.processing = false 
    self.save(:validations => false) 
    end 

    private 

    def continue_processing 
    if self.new_record? 
     !self.processing 
    end 
    end 

    def send_to_delayed_job 
    Delayed::Job.enqueue ImageProcess.new(self.id), :queue => 'paperclip' 
    end 
end 

型號/ image_process.rb

class ImageProcess < Struct.new(:picture_id) 

    def perform 
    picture = Picture.find(self.picture_id) 
    picture.process 
    end 

end 

如果我註釋掉after_create :send_to_delayed_jobbefore_post_process這兩行,即處理是在現場完成的,則自動定向過程起作用。但是,當我通過延遲工作時,沒有自動定位,只是調整大小。

有沒有人有任何想法?

編輯

它得到的陌生人。我搬到了Carrierwave和carrierwave_backgrounder的寶石。忽略後臺任務,現在,我在我的image_uploader.rb如下:

def auto_orient 
    manipulate! do |img| 
    img.auto_orient! 
    img 
    end 
end 

version :huge do 
    process :auto_orient 
    process resize_to_fit: [2048,1536] 
end 

這工作。圖像的方向正確。

現在,如果我按照載波wave_backgrounder的說明將process_in_background :image添加到我的picture.rb文件中,auto_orient不起作用。

我現在要去嘗試store_in_background方法,看看是否有所作爲。

+0

我使用 '轉換 「:源 - 自動定向:DEST」,來源:「#{File.expand_path(@文件路徑)}「,dest:File.expand_path(dst.path)' 而不是 'Paperclip.run('convert',」#{File.expand_path(@ file.path)} -auto-orient# {File.expand_path(dst.path)}「)' cuz有特殊符號和空格的文件 – faron 2013-10-09 11:21:16

回答

0

我注意到你的圖片模型中有attr_accessible。最近我一直在與Delayed_Job合作,經過幾個小時的努力,我發現它與attr_accessible有嚴重的問題。雖然有解決方法。

更多信息是here(儘管你可以谷歌更多的話題)