使用回形针的文件类型的自定义缩略图

发布于 2024-07-29 22:33:13 字数 725 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-08-05 22:33:13

我实际上已经实现了这个完全相同的功能。 Paperclip 为我的所有图像和 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

这将返回存储在S3上的缩略图的URL,或者返回通用的本地路径基于资产内容类型的 PNG 图标(如下所述)。 has_thumbnail? 方法确定该资源是否已为其生成缩略图。 这是我在我自己的 Paperclip 分支中添加的内容,但是您可以用自己的逻辑替换(我不确定确定这一点的“标准”方法,也许将该路径与您定义的“缺失”路径进行比较,甚至只是将内容类型与默认列表[“image/jpeg”、“image/png”]等进行比较。

无论如何,这是根据缩略图样式(在您的情况下为:thumb 和:large)和内容类型传回通用图标的路径的方法:

# 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/ 具有正确的文件名约定。 我的缩略图样式称为 :small,并且我已经为 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

如果不支持内容类型,则会显示一个通用的“catch all”图标:

icon.small.default.png

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 case Asset) I have defined the following method:

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

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:

# 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

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:

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

If the content type isn't supported, there's a generic 'catch all' icon which is displayed:

icon.small.default.png
梦中的蝴蝶 2024-08-05 22:33:13

您可以从您的资产继承一些文件类型,例如视频并指定不同的:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文