我已經實現了這個非常相同的功能。回形針爲我的所有圖像和PDF生成縮略圖,並且我爲MS Word,Excel,HTML,TXT文件等添加了自定義縮略圖圖標。
我的解決方案非常簡單。在我的模型Attachment
(你的情況Asset
)我已經定義了以下方法:
def thumbnail_uri(style = :original)
if style == :original || has_thumbnail?
attachment.s3.interface.get_link(attachment.s3_bucket.to_s, attachment.path(style), EXPIRES_AFTER)
else
generic_icon_path style
end
end
這將返回或者是URL存儲在S3的縮略圖,或基於內容的資產中的本地路徑到一個通用的PNG圖標類型(下面討論)。 has_thumbnail?
方法確定此資產是否有爲其生成的縮略圖。這是我在自己的Paperclip分支中添加的內容,但是您可以用自己的邏輯來替代(我不確定這種「標準」方式來確定此問題,可能會將路徑與您定義的「缺失」路徑進行比較,或者甚至只是將內容類型與默認列表[「image/jpeg」,「image/png」等)進行比較)。
總之,這裏的其傳遞迴基於兩種縮略圖式的通用圖標的路徑方法(在您的情況:拇指和:大)和內容類型:
# Generates a path to the thumbnail image for the given content type
# and image size.
#
# e.g. a :small thumbnail with a content type of text/html, the file name
# would have the filename icon.small.text.html.png
#
# If no such thumbnail can be found a generic one is returned
def generic_icon_path(style = image.default_style)
url = "/images/attachments/icon.#{style.to_s}.#{attachment_content_type.sub('/', '.')}.png"
if File.exists? "#{RAILS_ROOT}/public/#{url}"
url
else
"/images/attachments/icon.#{style.to_s}.default.png"
end
end
然後,加一個新的縮略圖,我只是用正確的文件名約定將PNG文件添加到/images/attachments/
。我thumbail風格被稱爲:小,我已經定義了Word,Excel和純文本樣式,以便在目前的時間,我有:
icon.small.application.msword.png
icon.small.text.plain.png
icon.small.application.vnd.ms-excel.png
icon.small.application.vnd.openxmlformats-officedocument.spreadsheetml.sheet.png
icon.small.application.vnd.openxmlformats-officedocument.wordprocessingml.document.png
如果內容類型不支持,有一個通用的「所有」圖標顯示:
icon.small.default.png
太棒了!非常感謝。我會在早上第一時間嘗試一下。似乎正是我想要做的。 – Chelsea 2009-07-31 03:26:51