实例公共方法
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 129 def preview(transformations) if previewable? ActiveStorage::Preview.new(self, transformations) else raise ActiveStorage::UnpreviewableError, "No previewer found for blob with ID=#{id} and content_type=#{content_type}" end end
previewable?() 链接
如果任何已注册的预览器接受 blob,则返回 true。默认情况下,这将对视频和 PDF 文档返回 true。
源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 138 def previewable? ActiveStorage.previewers.any? { |klass| klass.accept?(self) } end
representable?() 链接
如果 blob 是可变的或可预览的,则返回 true。
源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 163 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 151 def representation(transformations) case when previewable? preview transformations when variable? variant transformations else raise ActiveStorage::UnrepresentableError, "No previewer found and can't transform blob with ID=#{id} and content_type=#{content_type}" end end
variable?() 链接
如果变体处理器可以转换 blob(其内容类型在 `ActiveStorage.variable_content_types` 中),则返回 true。
源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 110 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 的变体,该变体将高度和宽度限制为 100 像素。然后,它将根据 blob 的派生键和转换将该变体上传到服务。
然而,通常您实际上并不想立即转换变体。而是简单地引用一个可以由控制器按需创建的特定变体。如下所示:
<%= image_tag Current.user.avatar.variant(resize_to_limit: [100, 100]) %>
这将为具有该特定变体的该特定 blob 创建一个 URL,然后 ActiveStorage::Representations::ProxyController 或 ActiveStorage::Representations::RedirectController 可以按需生成该 URL。
如果变体处理器无法转换 blob,则会引发 `ActiveStorage::InvariableError`。要确定一个 blob 是否可变,请调用 ActiveStorage::Blob#variable?。
选项¶ ↑
选项由 image_processing gem 定义,并取决于您使用的变体处理器:Vips 或 MiniMagick。但是,两个变体处理器都支持以下选项:
- :resize_to_limit
- 
将图像缩小到指定的尺寸内,同时保留原始宽高比。仅当图像大于指定尺寸时才会调整图像大小。 user.avatar.variant(resize_to_limit: [100, 100]) 
- :resize_to_fit
- 
将图像调整到指定的尺寸内,同时保留原始宽高比。如果图像大于指定尺寸,则会缩小图像;如果图像小于指定尺寸,则会放大图像。 user.avatar.variant(resize_to_fit: [100, 100]) 
- :resize_to_fill
- 
将图像调整到指定的尺寸内,同时保留原始宽高比。如有必要,将裁剪图像的较大尺寸。 user.avatar.variant(resize_to_fill: [100, 100]) 
- :resize_and_pad
- 
将图像调整到指定的尺寸内,同时保留原始宽高比。如有必要,如果源图像具有 alpha 通道,则用透明色填充剩余区域;否则用黑色填充。 user.avatar.variant(resize_and_pad: [100, 100]) 
- :crop
- 
从图像中提取一个区域。前两个参数是区域的左侧和顶部边缘,后两个参数是区域的宽度和高度。 user.avatar.variant(crop: [20, 50, 300, 300]) 
- :rotate
- 
将图像按指定角度旋转。 user.avatar.variant(rotate: 90) 
某些选项(包括上述选项)可以接受其他特定于处理器的值,这些值可以作为尾随的哈希传递。
<!-- Vips supports configuring `crop` for many of its transformations -->
<%= image_tag user.avatar.variant(resize_to_fill: [100, 100, { crop: :centre }]) %>
如果在 MiniMagick 和 Vips 之间迁移现有应用程序,您需要更新特定于处理器的选项。
<!-- MiniMagick -->
<%= image_tag user.avatar.variant(resize_to_limit: [100, 100], format: :jpeg,
      sampling_factor: "4:2:0", strip: true, interlace: "JPEG", colorspace: "sRGB", quality: 80) %>
<!-- Vips -->
<%= image_tag user.avatar.variant(resize_to_limit: [100, 100], format: :jpeg,
      saver: { subsample_mode: "on", strip: true, interlace: true, quality: 80 }) %>
            源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 100 def variant(transformations) if variable? variant_class.new(self, ActiveStorage::Variation.wrap(transformations).default_to(default_variant_transformations)) else raise ActiveStorage::InvariableError, "Can't transform blob with ID=#{id} and content_type=#{content_type}" end end