2012-08-10 30 views
12

で最初のPDFページのサムネイルを作成する方法:私はこのようにPDFファイルのサムネイルを処理してい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 

しかし、PDFファイルが複数ページあるとき、それは1つのJPGファイル内のすべてのページのサムネイルを生成します。 最初のページのサムネイルのみを作成する方法はありますか?

答えて

14

今年初めにpatchを提出しました。

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

process :cover 
2

この同じ問題の解決策を探すときは、この記事全体を実行しました。 pdfをjpegに変換すると、すべてのページがエンドツーエンドで結合された長いpdfが作成されるため、必要なアスペクト比に画像をトリミングして残りの部分を破棄する必要があります。以下は、私が使用して終了するものである:

コントローラ/ビューで
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 

私は、ユーザーエージェントの宝石を使用してこれをした:

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

Tanzeebの素晴らしいソリューション!ありがとうございました。

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 

素晴らしいのためにこれを使用:

は、だから私はこのような何かを行うことができます!

関連する問題