Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paperclip run processors on selected style

I have an :xxx image processor, and I have two styles in the model :big and :thumb.

How I can process with :xxx only the :thumb image leaving the :big image untouched ?

like image 519
astropanic Avatar asked Jul 22 '09 20:07

astropanic


3 Answers

I recently had a similar problem and found this solution on a message board. Hope it helps!

has_attached_file :screenshot,
 :default_style => :full,
 :styles => {
   :full => "280x210",
   :cropped => { :processors => [:screenshot_crop] }
 }
like image 104
Austin Avatar answered Nov 15 '22 15:11

Austin


By default, the Rake task refreshes all thumbnails. Keep in mind that it won't touch / process the original image.

You could have a look at the Rakefile and the Attachment class and modify to allow you to specify a specific thumbnail size, but the current design assumes that you want to take the original and redo all thumbnails from the original.

like image 34
mylescarrick Avatar answered Nov 15 '22 17:11

mylescarrick


Add this code to your paperclip.rake file:

   desc "Reprocesses your attachments style (set CLASS, ATTACHMENT and STYLE)"
    task :style => :environment do
      module JustForOneDay
        NAME = ENV['STYLE']
      end

      module ::Paperclip
        class Attachment
          def post_process_styles #:nodoc:
            @styles.each do |name, args|
              if JustForOneDay::NAME == name
                begin
                  raise RuntimeError.new("Style #{name} has no processors defined.") if args[:processors].blank?
                  @queued_for_write[name] = args[:processors].inject(@queued_for_write[:original]) do |file, processor|
                    Paperclip.processor(processor).make(file, args, self)
                  end
                rescue PaperclipError => e
                  log("An error was received while processing: #{e.inspect}")
                  (@errors[:processing] ||= []) << e.message if @whiny
                end
              end
            end
          end
        end
      end

      for_all_attachments do |instance, name|
        result = instance.send(name).reprocess!
      end
    end
  end

Tested with Paperclip 2.3.1.1

In Paperclip 2.3.3 this should be:

def post_process_styles #:nodoc:
  styles.each do |name, style|
    if JustForOneDay::NAME == name
    begin
      raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
      @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
        Paperclip.processor(processor).make(file, style.processor_options, self)
      end
    rescue PaperclipError => e
      log("An error was received while processing: #{e.inspect}")
      (@errors[:processing] ||= []) << e.message if @whiny
    end
    end
  end
end

It's easy, just go to attachment.rb file in your paperclip version.

like image 1
Dmitry Polushkin Avatar answered Nov 15 '22 16:11

Dmitry Polushkin