views:

555

answers:

5

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 ?

A: 

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.

mylescarrick
And is there an other option to crop only the :thumb image leaving the others alone ? I mean without modifing the Attachment class ?Maybe some extra code in the model / controller ?
astropanic
A: 

But what if I have three styles and only what to run the processor on one of them?

Minver
A: 

I kludged this--it's not elegant, but it worked for me.

One of your styles should have dimensions different from all the other styles. In this way, in your custom Paperclip Processor, you can see if the contents of the command string contain the given dimensions. If so you would do special processing, if not, you would not.

(I clipped this code -- and modified it -- from Ryan Bate's Railscast Episode 182.)

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      SPECIAL_PROCESSING_FLAG = "150x150"
      if crop_command && super.include?(SPECIAL_PROCESSING_FLAG)
        crop_command + super.sub(/ -crop \S+/, '')
      else
        super 'do nothing fancy
      end
    end

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

In my situation it didn't matter that we reprocessed in the non-special case too, since the end result changed nothing.

Mario
+1  A: 

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] }
 }
Austin
A: 

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.

Dmitry Polushkin