Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用曲别针的文件类型的自定义缩略图_Ruby On Rails_Image_File Upload_Paperclip_Thumbnails - Fatal编程技术网

Ruby on rails 使用曲别针的文件类型的自定义缩略图

Ruby on rails 使用曲别针的文件类型的自定义缩略图,ruby-on-rails,image,file-upload,paperclip,thumbnails,Ruby On Rails,Image,File Upload,Paperclip,Thumbnails,我使用带有Ruby on Rails的回形针将资产附加到模型上,这些资产可以是任何文件类型,并且当前仅当资产是图像时才会生成缩略图。我希望能够为其他文件显示不同的默认图像,或者在上传时生成文件的缩略图,或者使用默认的\u url设置一些内容,但是到目前为止,我找不到任何资源来帮助我实现这一点,我自己也没有任何进展 我的模型如下: class Asset < ActiveRecord::Base has_attached_file :media, :storage

我使用带有Ruby on Rails的回形针将资产附加到模型上,这些资产可以是任何文件类型,并且当前仅当资产是图像时才会生成缩略图。我希望能够为其他文件显示不同的默认图像,或者在上传时生成文件的缩略图,或者使用默认的\u 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"  
class资产:s3,
:s3_credentials=>“#{RAILS_ROOT}/config/s3.yml”,
:path=>“:attachment/:id/:style.:extension”,
:bucket=>S3_bucket,
:style=>{:thumb=>“75x75>”,:large=>“600x800>”,
:呜呜=>错,
:default_url=>“/images/:attachment/missing.jpg”
如果生成失败,或者在默认url中使用:content\u type之类的方法生成自定义缩略图,是否有人有任何资源?我已经查看了源代码,但没有找到任何地方


谢谢!

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

是否已附加文件:媒体,…:样式=>{….}


请看本教程。

我实际上已经实现了这个非常相同的功能。回形针为我的所有图像和PDF生成缩略图,我还为MS Word、Excel、HTML、TXT文件等添加了自定义缩略图图标

我的解决方案相当简单。在我的模型
附件
(在您的案例中
资产
)中,我定义了以下方法:

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图标的本地路径(如下所述).The
has_thumbnail?
方法确定此资产是否已生成缩略图。这是我在自己的回形针叉中添加的内容,但您可以在自己的逻辑中替换(我不确定确定确定这一点的“标准”方法,可能是将路径与定义的“缺失”路径进行比较,甚至只是将内容类型与默认列表进行比较[“图像/jpeg”、“图像/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
如果不支持该内容类型,则会显示一个通用的“全面覆盖”图标:

icon.small.default.png

太棒了!非常感谢。我会在早上第一件事就尝试一下。这似乎正是我想要做的。再次感谢,在大约10分钟内就完成了,并且运行得很好。非常感谢您的帮助。现在有一种方法可以调用附件从s3获取过期链接:附件。过期url(超时,样式)例如,attachment.expiring_url(60,:thumb),因此在缩略图_uri中,您可以使用它来代替调用attachment.s3.interface.get_链接