Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create thumbnail for first pdf page with carrierwave

I'm processing thumbnail for PDF file in this way:

  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

But when PDF file is multipage it produces thumbnail for all pages in one jpg file. Is there any way to produce thumbnail only for first page?

like image 543
Arthur Avatar asked Aug 10 '12 08:08

Arthur


2 Answers

I submitted a patch earlier this year to do just this. Use a custom processor:

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

process :cover
like image 181
Tanzeeb Khalili Avatar answered Oct 25 '22 22:10

Tanzeeb Khalili


Great solution by Tanzeeb! Thank you.

So i could do something like this:

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

and used this for the thumb generation

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

great!

like image 26
everyman Avatar answered Oct 25 '22 20:10

everyman