使用延迟作业时自动定向图像

时间:2013-04-05 14:27:08

标签: ruby-on-rails imagemagick paperclip carrierwave delayed-job

我有一个照片共享应用程序,允许用户拖放图像,然后在延迟作业中处理并显示在图库中。我在解决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 gem。暂时忽略后台任务,我在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方法,看看是否有所作为。

1 个答案:

答案 0 :(得分:0)

我注意到你的图片模型中有attr_accessible。我最近一直在和Delayed_Job合作,经过几个小时的努力,我发现它与attr_accessible存在严重问题。但是有一些解决方法。

更多信息是here(尽管您可以通过Google获取有关该主题的更多信息)

相关问题