2010-12-14 18 views
20

मैंने हाल ही में रेल के साथ पेपरक्लिप लागू किया है और 0Mजैसे ImageMagick से कुछ फ़िल्टर विकल्पों को आजमाएं। मैं ऐसा करने के तरीके के बारे में कोई उदाहरण नहीं ढूंढ पाया। क्या यह एक और विकल्प के रूप में शैली के माध्यम से पारित हो जाता है?Rails पेपरक्लिप ImageMagick के फ़िल्टर विकल्पों का उपयोग कैसे करें?

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

@ Plang के जवाब सही था, लेकिन मैं कलंक को सटीक समाधान देना चाहता था, बस में मामला किसी देख रहा था और इस सवाल का पाया:

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

कौन इस बदल दिया है:
alt text

इस के लिए

:
alt text

उत्तर

13

मैं यह परीक्षण नहीं किया, लेकिन आप इस तरह से, "convert_options" पैरामीटर का उपयोग करने के लिए सक्षम होना चाहिए:

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

https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb

पर एक नज़र मैं personnaly मेरे अपने प्रोसेसर का उपयोग है।

मॉडल में:

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

lib में:

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 

यह एक सरल ग्रेस्केल रूपांतरण के लिए 100% आवश्यक नहीं हो सकता है, लेकिन यह काम करता है!

+2

देरी के लिए खेद है, धन्यवाद! – jyoseph

+4

अच्छी तरह से लगता है कि कन्वर्ट विकल्प ': शैलियों => {: ग्रे =>" 450x250 "} में परिवर्तित करना आसान है,: convert_options => {: gray =>" -blur 0x8 "}' – Ben

0

रेल 5, पेपरक्लिप 5 अद्यतन

इसके बजाय अब एक पुस्तकालय जोड़ने के लिए होने के

, तो आप सिर्फ बाहर ImageMagick's convert command करने के लिए सिस्टम पर कॉल अपने grayscale option उपयोग करने के लिए कर सकते हैं। आप धुंध या किसी अन्य छवि मैजिक विकल्पों के लिए भी ऐसा ही कर सकते हैं, लेकिन मुझे ग्रेस्केल में रूपांतरण के लिए ऐसा करने की आवश्यकता है।

अपने मॉडल में (ग्राहक एक लोगो है):

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 

तो बस (Paperclips github docs प्रति) इस तरह ध्यान में रखते हुए का उपयोग करें।

आपके विचार में:

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

या एक लिंक यदि आप पसंद के साथ: महान उत्तर के लिए

<%= link_to(image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name), client.url) %> 
संबंधित मुद्दे