使用回形针的文件类型的自定义缩略图
我使用 Paperclip 和 Ruby on Rails 将资源附加到模型,这些资源可以是任何文件类型,并且当前仅当资源是图像时才会生成缩略图。 我希望能够为其他文件显示不同的默认图像,可以通过在上传时生成文件的缩略图,或者使用 default_url 设置某些内容,但到目前为止我找不到任何资源来帮助解决此问题我自己一事无成。
我的模型如下:
class Asset < ActiveRecord::Base
has_attached_file :media,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => S3_BUCKET,
:styles => {:thumb => "75x75>", :large => "600x800>",
:whiny => false,
:default_url => "/images/:attachment/missing.jpg"
如果生成失败,是否有人有任何资源可以生成自定义缩略图,或者依靠默认网址中的 :content_type 之类的资源? 我已经查看了源代码,但没有找到任何地方。
谢谢!
I'm using Paperclip with a Ruby on Rails to attach assets to a model, these assets can be any file type and currently thumbnails are only being generated if the asset is an image. I'd like to be able to display a different default image for other files, either by generating a thumbnail of the files on upload, or setting something up with the default_url but so far I can't find any resources to help with this and am getting no where on my own.
My model is as follows:
class Asset < ActiveRecord::Base
has_attached_file :media,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => S3_BUCKET,
:styles => {:thumb => "75x75>", :large => "600x800>",
:whiny => false,
:default_url => "/images/:attachment/missing.jpg"
Does anyone have any resources for generating custom thumbnails if generation fails, or fall back on something like :content_type in the default url? I've looked through the source and haven't been able to get anywhere.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上已经实现了这个完全相同的功能。 Paperclip 为我的所有图像和 PDF 生成缩略图,并且我为 MS Word、Excel、HTML、TXT 文件等添加了自定义缩略图图标。
我的解决方案相当简单。 在我的模型
Attachment
(在您的情况下为Asset
)中,我定义了以下方法:这将返回存储在S3上的缩略图的URL,或者返回通用的本地路径基于资产内容类型的 PNG 图标(如下所述)。
has_thumbnail?
方法确定该资源是否已为其生成缩略图。 这是我在我自己的 Paperclip 分支中添加的内容,但是您可以用自己的逻辑替换(我不确定确定这一点的“标准”方法,也许将该路径与您定义的“缺失”路径进行比较,甚至只是将内容类型与默认列表[“image/jpeg”、“image/png”]等进行比较。无论如何,这是根据缩略图样式(在您的情况下为:thumb 和:large)和内容类型传回通用图标的路径的方法:
然后,要添加新的缩略图,我只需将 PNG 文件添加到
/images/attachments/
具有正确的文件名约定。 我的缩略图样式称为 :small,并且我已经为 Word、Excel 和纯文本定义了样式,因此目前我有:如果不支持内容类型,则会显示一个通用的“catch all”图标:
I've actually implemented this very same feature. Paperclip generates thumbnails for all my images and PDFs, and I have added custom thumbnail icons for MS Word, Excel, HTML, TXT files etc.
My solution is fairly straightforward. In my model
Attachment
(in your caseAsset
) I have defined the following method:This returns either a URL to a thumbnail stored on S3, or a local path to a generic PNG icon based on the assets content type (discussed below). The
has_thumbnail?
method determines whether or not this asset has had a thumbnail generated for it. This is something I added in my own fork of Paperclip, but you can substitute in your own logic (I'm not sure of the 'standard' way to determine this, maybe comparing the path with your defined 'missing' path, or even just comparing the content type to a default list ["image/jpeg", "image/png"] etc).Anyway, here's the method which passes back a path to a generic icon based on both the thumbnail style (in your case :thumb and :large) and the content type:
Then, to add a new thumbnail I just add PNG files into
/images/attachments/
with the correct file name convention. My thumbail style is called :small and I have defined styles for Word, Excel and plain text so at the present time I have:If the content type isn't supported, there's a generic 'catch all' icon which is displayed:
您可以从您的资产继承一些文件类型,例如视频并指定不同的:
has_attached_file :media, ..., :style => {....}
看看这个教程 视频缩略图。
You could have some file types inherit from your Asset, e.g. Video and specify a different:
has_attached_file :media, ..., :style => {....}
Have a look at this tutorial for video thumbnails.