2013-05-27 44 views
5

Tôi đã cài đặt carrierwave trong ứng dụng đường ray của mình. Tuy nhiên, khi người dùng tải lên một tệp PDF đa phần, tôi chỉ muốn ứng dụng lấy trang đầu tiên trong tài liệu và chuyển đổi nó thành tệp jpeg. Điều này có thể, và với lệnh nào?CarrierWave - PDF - Chỉ chọn trang đầu tiên

Đây là người tải lên của tôi.

# encoding: utf-8 

class ImageUploader < CarrierWave::Uploader::Base 

    # Include RMagick or MiniMagick support: 
    include CarrierWave::RMagick 
    include CarrierWave::MimeTypes 
    # include CarrierWave::MiniMagick 

    # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: 
    include Sprockets::Helpers::RailsHelper 
    include Sprockets::Helpers::IsolatedHelper 

    # Choose what kind of storage to use for this uploader: 
    storage :file 
    # storage :fog 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    # def default_url 
    # # For Rails 3.1+ asset pipeline compatibility: 
    # # asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 
    # 
    # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 
    # end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 
    # Create different versions of your uploaded files: 
    version :thumb do 
    process :resize_to_fill => [150, 210] 
    process :convert => :jpg 

    def full_filename (for_file = model.source.file) 
     super.chomp(File.extname(super)) + '.jpg' 
    end 
    end 

    version :thumb_big do 
    process :resize_to_fill => [320, 440] 
    process :convert => :jpg 

    def full_filename (for_file = model.source.file) 
     super.chomp(File.extname(super)) + '.jpg' 
    end 
    end 

    version :normal do 
    process :resize_to_fill => [450, 630] 
    process :convert => :jpg 

    def full_filename (for_file = model.source.file) 
     super.chomp(File.extname(super)) + '.jpg' 
    end 
    end 

    # Add a white list of extensions which are allowed to be uploaded. 
    # For images you might use something like this: 
    # def extension_white_list 
    # %w(jpg jpeg gif png) 
    # end 

    # Override the filename of the uploaded files: 
    # Avoid using model.id or version_name here, see uploader/store.rb for details. 
    # def filename 
    # "something.jpg" if original_filename 
    # end 

end 

Trả lời

10

Tôi nghĩ rằng công trình này dành cho bạn:

read more

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


version :thumb do 
    process :cover 
    process :resize_to_fill => [150, 210] 
    process :convert => :jpg 

    def full_filename (for_file = model.source.file) 
     super.chomp(File.extname(super)) + '.jpg' 
    end 
    end 

version :thumb_big do 
    process :cover 
    process :resize_to_fill => [320, 440] 
    process :convert => :jpg 

    def full_filename (for_file = model.source.file) 
    super.chomp(File.extname(super)) + '.jpg' 
    end 
end 

version :normal do 
    process :cover 
    process :resize_to_fill => [450, 630] 
    process :convert => :jpg 

    def full_filename (for_file = model.source.file) 
    super.chomp(File.extname(super)) + '.jpg' 
    end 
end 
+0

Nó hoạt động hoàn hảo, cảm ơn! =) – Philip

+0

Bất cứ ai cũng có một giải pháp tương tự cho MiniMagick? – hellion

+2

@hellion Tôi đã có thể làm cho nó hoạt động chỉ bằng cách tinh chỉnh bìa như sau: def cover pdf = MiniMagick :: Image.open (self.file.path) pdf.pages.first end – Blago

Các vấn đề liên quan