我想让 Paperclip 为上传的多页 PDF 文件的每一页创建 2 个缩略图。
我正在运行 Paperclip 2.3.1.1 并在我的资产模型中使用它:
has_attached_file :asset,
:styles => { :medium => "800x600>", :thumb => "100x100>" }
因此,当我上传 3 页 pdf 文件时,我希望这会在每页创建 2 个拇指(一个为 800x600,另一个为 100x100)。相反,我创建了 3 个文件夹(thumb、medium、original)——原始文件夹包含原始 pdf 文件,而thumb 和medium 各包含一个 pdf,其中只有 pdf 的第一页全部像素化。
我需要做什么才能让回形针为 pdf 的每一页创建 2 个拇指?理想情况下,我希望每页一张图像,如下所示(创建 6 张图像):
资产/1/medium/file-0.png
资产/1/medium/file-1.png
资产/1/medium/file-2.png
资产/1/thumb/file-0.png
资产/1/thumb/file-1.png
资产/1/thumb/file-2.png
有谁知道如何做到这一点?我需要定制处理器吗?如果是这样,处理器会是什么样子?
Thanks.
这是我如何实现类似任务的。
文档模型:
class Document < ActiveRecord::Base
has_many :pages, :dependent => :destroy
has_attached_file :asset
after_asset_post_process :make_pages
private
def make_pages
if valid?
Paperclip.run('convert', "-quality #{Page::QUALITY} -density #{Page::DENSITY} #{asset.queued_for_write[:original].path} #{asset.queued_for_write[:original].path}%d.png")
images = Dir.glob("#{asset.queued_for_write[:original].path}*.png").sort_by do |line|
line.match(/(\d+)\.png$/)[1].to_i
end
images.each do |page_image|
pages.build(:asset => File.open(page_image))
end
FileUtils.rm images
end
end
end
页面模型:
class Page < ActiveRecord::Base
belongs_to :document
has_attached_file :asset
QUALITY = 100
DENSITY = '80x80'
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)