Domanda

Ho un processore di immagini :xxx e ho due stili nel modello :big e :thumb.

Come posso elaborare con :xxx solo l'immagine :thumb lasciando intatta l'immagine :big?

È stato utile?

Soluzione

Per impostazione predefinita, l'attività Rake aggiorna tutte le miniature.Tieni presente che non toccherà/elaborerà l'immagine originale.

Potresti avere un guarda il Rakefile e il Allegato class e modificarla per consentirti di specificare una dimensione di miniatura specifica, ma il progetto corrente presuppone che tu voglia prendere l'originale e ripetere tutte le miniature dall'originale.

Altri suggerimenti

Recentemente ho avuto un problema simile e ho trovato questa soluzione su una bacheca.Spero che sia d'aiuto!

has_attached_file :screenshot,
 :default_style => :full,
 :styles => {
   :full => "280x210",
   :cropped => { :processors => [:screenshot_crop] }
 }

Aggiungi questo codice al tuo file paperclip.rake:

   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

Testato con Paperclip 2.3.1.1

In Paperclip 2.3.3 questo dovrebbe essere:

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

È semplice, basta accedere al file attach.rb nella versione a graffetta.

L'ho pensato: non è elegante, ma per me ha funzionato.

Uno dei tuoi stili dovrebbe avere dimensioni diverse da tutti gli altri stili.In questo modo, nel tuo processore Paperclip personalizzato, puoi vedere se il contenuto della stringa di comando contiene le dimensioni specificate.Se così fosse, eseguiresti un'elaborazione speciale, in caso contrario no.

(Ho ritagliato questo codice e modificato dall'episodio 182 di Railscast di Ryan Bate.)

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

Nella mia situazione non aveva importanza la rielaborazione anche nel caso non speciale, poiché il risultato finale non cambiava nulla.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top