2013-04-16 160 views
1

我有一個問題,爲PDF文件創建JPG縮略圖。Carrierwave PDF格式轉換爲JPG(MiniMagick)

我正在使用Carrierwave。 Imagemagick已正確安裝。創建PNG,JPG或GIF文件的縮略圖沒有問題。

這裏是我的代碼使用

version :thumb, :if => :image? do 
    storage :file 

     begin 
      process :convert => 'jpg' # this should do the trick but doesn't 
      process :resize_to_fill => [160,160] 
      process :quality => 95 
      process :set_content_type 
      process :set_thumb_attr => true 
      def full_filename (for_file = model.file.file) 
       "#{model.slug}-thumb.jpg" 
      end 
     rescue Exception => e 
      puts e.inspect 
      true 
     end 
end 

def set_thumb_attr(val) 
    model.thumb = val 
end 


def image?(new_file) 
    model.mime.include? 'image/' or model.mime.include? '/pdf' 
end 

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

縮略圖的內容表明,所創建的文件仍然是一個PDF文件。

%PDF-1.3 1 0 OBJ < < /頁2 0 R /類型/目錄 ...

我明白任何建議/解決方案。

回答

0

問題是

process :quality => 95 

它重新處理PDF。刪除該行解決了我的問題。