使用Paperclip为文件类型定制缩略图 [英] Custom thumbnails for file types with Paperclip

查看:189
本文介绍了使用Paperclip为文件类型定制缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ruby on Rails上使用Paperclip将资源附加到模型上,这些资源可以是任何文件类型,只有在资产是图片时才会生成缩略图。我希望能够为其他文件显示不同的默认图像,或者通过在上传文件中生成缩略图,或者使用default_url设置某些内容,但到目前为止,我找不到任何资源来帮助这个我的模型如下:

  class资产< 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>,:大=> 600x800>,
:whiny =>假,
:default_url => /images/:attachment/missing.jpg

有没有人有资源来生成自定义缩略图if代失败,或回落在默认的网址中的内容类型:content_type?



谢谢!

解决方案

我已经实现了这个非常相同的功能。回形针为我的所有图像和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]等)进行比较。

无论如何,这是传回路径的方法到一个基于缩略图风格(在你的情况:拇指和:大)和内容类型的通用图标:

 #为给定内容类型
#和图像大小生成缩略图的路径。

#例如a:内容类型为text / html的小缩略图,文件名
#将具有文件名icon.small.text.html.png

#如果没有这样的缩略图可以发现一个通用的返回
def generic_icon_path(style = image.default_style)
url =/images/attachments/icon.#{style.to_s}.#{attachment_content_type.sub('/', '。')}。png
如果File.exists? #{RAILS_ROOT} / public /#{url}
url
else
/images/attachments/icon.#{style.to_s}.default.png
结束
结束

然后,添加一个新的缩略图,我只是将PNG文件添加到 / images / attachments / ,使用正确的文件名称约定。我的缩略图风格被称为:小,我已经定义了Word,Excel和纯文本的样式,所以现在我有:

pre $ 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 


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!

解决方案

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

这篇关于使用Paperclip为文件类型定制缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆