2012-08-10 9 views
12

मैं इस तरह से पीडीएफ फाइल के लिए थंबनेल प्रसंस्करण कर रहा हूँ के साथ पहली पीडीएफ पृष्ठ के लिए थंबनेल बनाने के लिए कैसे:carrierwave

version :thumb do 
    process :resize_to_limit => [260, 192] 
    process :convert => :jpg 
    process :set_content_type 
    end 

    def set_content_type(*args) 
    self.file.instance_variable_set(:@content_type, "image/jpeg") 
    end 

लेकिन जब पीडीएफ फाइल मल्टीपेज है यह एक jpg फ़ाइल में सभी पृष्ठों के थंबनेल पैदा करता है। क्या पहले पृष्ठ के लिए थंबनेल बनाने का कोई तरीका है?

उत्तर

14

मैंने इस साल की शुरुआत में patch सबमिट किया था। एक कस्टम प्रोसेसर का उपयोग करें:

def cover 
    manipulate! do |frame, index| 
    frame if index.zero? 
    end 
end 

process :cover 
2

इस समस्या के समाधान के लिए खोज करते समय मैं इस पोस्ट में भाग गया। जब आप एक पीडीएफ को जेपीईजी में परिवर्तित करते हैं तो यह अंत तक जुड़े सभी पृष्ठों के साथ एक लंबा पीडीएफ बनाता है, इसलिए आपको छवि को उस पहलू अनुपात में फसल करने की आवश्यकता होती है जिसे आप चाहते हैं और बाकी को छोड़ दें।

version :thumb_safari do #special version for safari and ios 
    process :resize_to_fit => [200,200] 
    process :convert => 'jpg' 
    process :paper_shape 
    def full_filename (for_file = model.logo.file) 
    super.chomp(File.extname(super)) + '.jpg' 
    end  
end 

version :thumb do #all browsers except safari 
    process :resize_to_fit => [200,200] 
    process :convert => 'jpg' #must convert to jpg before running paper shape 
    process :paper_shape 
    process :convert => 'jpg' #after running paper_shape it will default to original file type 
    def full_filename (for_file = model.logo.file) 
    super.chomp(File.extname(super)) + '.jpg' 
    end 
end 

def paper_shape 
    manipulate! do |img| 
    if img.rows*4 != img.columns*3 
     width=img.columns 
     height=img.columns/3*4 
     img.crop!(0,0,width,height,true) 
    else 
     img 
    end 
    end 
end 

नियंत्रक/दृश्य मैं UserAgent मणि इस्तेमाल किया और ऐसा किया: नीचे क्या मैं का उपयोग कर समाप्त हो गया है

documents_controller.rb

def index 
    @user_agent=UserAgent.parse(request.user_agent) 
    @search = Document.search(params[:q]) 
end 

index.html.rb

<% if @user_agent.browser.downcase == 'safari' %> 
<%= link_to(image_tag(doc.pdfdoc_url(:thumb_safari).to_s, :class=>"dropshadow", :size => "150x225"), doc.pdfdoc_url)%> 
<% else %> 
<%= link_to(image_tag(doc.pdfdoc_url(:thumb).to_s, :class=>"dropshadow", :size => "150x225"), doc.pdfdoc_url)%> 
<% end %> 

इसमें कोई संदेह नहीं है कि ऐसा करने का एक बेहतर तरीका है लेकिन यह अभी काम कर रहा है।

7

तंजेब द्वारा महान समाधान! धन्यवाद।

तो मैं कुछ इस तरह कर सकता है:

def cover 
    manipulate! do |frame, index| 
     frame if index.zero? 
    end 
    end 

और अंगूठे पीढ़ी

version :thumb do 
    process :cover  
    process :resize_to_fill => [50, 50, Magick::NorthGravity] 
    process :convert => 'png' 
    end 

महान के लिए इस प्रयोग किया जाता है!

संबंधित मुद्दे