实例公共方法
preview(transformations) 链接
返回一个 ActiveStorage::Preview
实例,其中包含提供的 transformations
集合。预览是从非图像 blob 生成的图像。Active Storage 为视频和 PDF 文档提供了内置预览器。视频预览器从视频中提取第一帧,PDF 预览器从 PDF 文档中提取第一页。
blob.preview(resize_to_limit: [100, 100]).processed.url
避免在视图中同步处理预览。相反,链接到按需处理预览的控制器操作。Active Storage 提供了一个,但你可能希望创建自己的(例如,如果你需要身份验证)。以下是如何使用内置版本
<%= image_tag video.preview(resize_to_limit: [100, 100]) %>
如果没有任何预览器接受接收 blob,此方法将引发 ActiveStorage::UnpreviewableError
。要确定 blob 是否被任何预览器接受,请调用 ActiveStorage::Blob#previewable?
。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 63 def preview(transformations) if previewable? ActiveStorage::Preview.new(self, transformations) else raise ActiveStorage::UnpreviewableError end end
previewable?() 链接
如果任何已注册的预览器接受 blob,则返回 true。默认情况下,这会对视频和 PDF 文档返回 true。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 72 def previewable? ActiveStorage.previewers.any? { |klass| klass.accept?(self) } end
representable?() 链接
如果 blob 可变或可预览,则返回 true。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 97 def representable? variable? || previewable? end
representation(transformations) 链接
为可预览 blob 返回 ActiveStorage::Preview
,为可变图像 blob 返回 ActiveStorage::Variant
。
blob.representation(resize_to_limit: [100, 100]).processed.url
如果接收 blob 既不可变也不可预览,则引发 ActiveStorage::UnrepresentableError
。调用 ActiveStorage::Blob#representable?
以确定 blob 是否可表示。
有关详细信息,请参阅 ActiveStorage::Blob#preview
和 ActiveStorage::Blob#variant
。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 85 def representation(transformations) case when previewable? preview transformations when variable? variant transformations else raise ActiveStorage::UnrepresentableError end end
variable?() 链接
如果变体处理器可以转换 blob(其内容类型在 ActiveStorage.variable_content_types
中),则返回 true。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 44 def variable? ActiveStorage.variable_content_types.include?(content_type) end
variant(transformations) 链接
返回一个 ActiveStorage::Variant
或 ActiveStorage::VariantWithRecord
实例,其中包含所提供的 transformations
集合。这仅与图像文件相关,它允许转换任何图像的大小、颜色等。示例
avatar.variant(resize_to_limit: [100, 100]).processed.url
这将创建并处理一个头像 blob 的变体,该变体的宽高限制为 100px。然后,它会根据 blob 的派生键和转换将所述变体上传到服务。
但是,通常情况下,您实际上并不想立即转换变体。而只是简单地引用一个特定的变体,该变体可以由控制器按需创建。如下所示
<%= image_tag Current.user.avatar.variant(resize_to_limit: [100, 100]) %>
这将为该特定 blob 创建一个 URL,其中包含该特定变体,然后 ActiveStorage::RepresentationsController 可以按需生成该 URL。
如果变体处理器无法转换 blob,则引发 ActiveStorage::InvariableError
。要确定 blob 是否可变,请调用 ActiveStorage::Blob#variable?
。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 34 def variant(transformations) if variable? variant_class.new(self, ActiveStorage::Variation.wrap(transformations).default_to(default_variant_transformations)) else raise ActiveStorage::InvariableError end end