我有一個半工作的解決方案...但這不是很優雅。我真的想提出更好的建議,但我想我會分享。
我開始時定義了一堆新樣式,每個頁面一個樣式......一直到我希望能夠處理的很多頁面。 (愚蠢的,我知道,但我不知道如何訪問路徑插值的曲別針,使每個頁面獲取保存/店裏正確刪除,除非對每個圖像的獨特風格)
{ ...
:page_0 => {:geometry=>'800[0]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_1 => {:geometry=>'800[1]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_2 => {:geometry=>'800[2]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_3 => {:geometry=>'800[3]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_4 => {:geometry=>'800[4]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_5 => {:geometry=>'800[5]', :format=>:png, :processors=>[:multipage_thumbnail]},
}
然後...我有一個自定義處理器,從縮略圖處理器的子類,有一些額外的邏輯用於運行帶有正確頁面的轉換命令#。
module Paperclip
# Handles thumbnailing images that are uploaded.
class MultipageThumbnail < Thumbnail
# Creates a Thumbnail object set to work on the +file+ given. It
# will attempt to transform the image into one defined by +target_geometry+
# which is a "WxH"-style string. +format+ will be inferred from the +file+
# unless specified. Thumbnail creation will raise no errors unless
# +whiny+ is true (which it is, by default. If +convert_options+ is
# set, the options will be appended to the convert command upon image conversion
def initialize file, options = {}, attachment = nil
@page = options[:geometry].match(/\[(\d+)\]/)[1] rescue 0
@page ||= 0
options[:geometry] = options[:geometry].sub(/\[\d+\]/, '')
super
end
# Performs the conversion of the +file+ into a thumbnail. Returns the Tempfile
# that contains the new image.
def make
return nil if @page >= page_count
src = @file
dst = Tempfile.new([@basename, @format].compact.join("."))
dst.binmode
begin
options = [
source_file_options,
"#{ File.expand_path(src.path) }[#{@page}]",
transformation_command,
convert_options,
"#{ File.expand_path(dst.path) }"
].flatten.compact
success = Paperclip.run("convert", *options)
rescue PaperclipCommandLineError => e
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
end
dst
end
def page_count
@page_count ||= begin
files = Paperclip.run("identify", "#{@file.path}")
files.split(/\n/).size
rescue PaperclipCommandLineError
1
end
end
end
end
我已經嘗試這種解決方案,但convert命令似乎只產生第一頁1個圖像。否則它效果很好。有任何想法嗎? – 2013-07-04 05:38:08
您可以使用來自終端中的Imagemagick軟件包的「convert」命令來進行調試。 – taro 2013-07-04 10:08:26
謝謝,我一直在玩這個。仍然沒有得到它的工作,只爲第一頁生成1張圖片。 – 2013-07-04 18:58:32