2010-12-14 19 views
20

Kısa bir süre önce Paperclip'i Rails ile uyguladım ve ImageMagick'ten blur gibi bazı filtre seçeneklerini denemek istiyorum. Bunun nasıl yapılacağına dair herhangi bir örnek bulamadım. Geçilir mi: stil başka bir seçenek olarak mı?Raylar Paperclip ImageMagick'in filtre seçeneklerini nasıl kullanır?

:styles => { :medium => "300x300#", :thumb => "100x100#" } 

plang cevabı doğru ama dava birisi arıyordu sadece, bulanıklık kesin çözüm vermek istedim ve bu soruyu buldum @:

:convert_options => { :all => "-blur 0x8" } 
// -blur {radius}x{sigma} 

bu değişti:
Buna alt text

:
alt text

cevap

13

Bunu test etmedi, ama böyle, "convert_options" parametre kullanmak mümkün olmalıdır:

:convert_options => { :all => ‘-colorspace Gray’ } 

Ben bizzat kendi işlemcisini kullanmak https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb

göz at. Bu basit bir gri tonlama dönüşüm için% 100 gerekli olmayabilir

module Paperclip 
    # Handles grayscale conversion of images that are uploaded. 
    class Grayscale < Processor 

    def initialize file, options = {}, attachment = nil 
     super 
     @format = File.extname(@file.path) 
     @basename = File.basename(@file.path, @format) 
    end 

    def make 
     src = @file 
     dst = Tempfile.new([@basename, @format]) 
     dst.binmode 

     begin 
     parameters = [] 
     parameters << ":source" 
     parameters << "-colorspace Gray" 
     parameters << ":dest" 

     parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") 

     success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path)) 
     rescue PaperclipCommandLineError => e 
     raise PaperclipError, "There was an error during the grayscale conversion for #{@basename}" if @whiny 
     end 

     dst 
    end 

    end 
end 

, ama işe yarıyor: lib olarak

has_attached_file :logo, 
        :url => PaperclipAssetsController.config_url, 
        :path => PaperclipAssetsController.config_path, 
        :styles => { 
           :grayscale => { :processors => [:grayscale] } 
           } 

: Modeli'nde

!

+2

Maalesef sayesinde:

<%= image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name) %> 

veya dilerseniz bağlantı ile

! – jyoseph

+4

Dönüştürme seçeneklerinde ': styles => {: grey =>" 450x250 "},: convert_options => {: grey =>" -blur 0x8 "}' 'dır. – Ben

0

Raylar 5, Paperclip 5 güncellemesi yerine şimdi bir kütüphane eklemek zorunda

, sadece onun grayscale option kullanmak için sistemde ImageMagick's convert command dışarı çağırabilirsiniz. Görüntü bulanıklığı veya diğer ImageMagick seçeneklerinden herhangi birini yapabilirsiniz, ancak bunu gri tonlamaya dönüştürmek için yapmam gerekiyor. senin modelde

(bir logo vardır istemci):

class Client < ApplicationRecord 
    has_attached_file :logo, 
        styles: { thumb: "243x243#", grayscale: "243x243#" } 
    # ensure it's an image 
    validates_attachment_content_type :logo, content_type: /\Aimage\/.*\z/ 

    # optional, just for name and url to be required 
    validates :name, presence: true 
    validates :url, presence: true 

    after_save :convert_grayscale 

    def convert_grayscale 
    system "convert #{self.logo.path(:thumb)} -grayscale Rec709Luminance #{self.logo.path(:grayscale)}" 
    end 

    def logo_attached? 
    self.logo.file? 
    end 
end 

Sonra sadece ( Paperclips github docs başına) böyle görünümünde kullanın. Size göre

: Büyük cevap için gecikme için

<%= link_to(image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name), client.url) %> 
İlgili konular