我对此有一个半有效的解决方案......但它不是很优雅。我真的很想想出更好的东西,但我想我还是要分享。
我首先定义了一堆新样式,每个页面一个样式... 最多我希望能够处理多少个页面。 (愚蠢,我知道,但我不知道如何访问 Paperclip 中的路径插值,以便在商店中正确保存/删除每个页面,除非每个图像都有独特的样式)
{ ...
: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