跳至内容 跳至搜索

Active Storage 变体

图像 Blob 可以有变体,这些变体是对原始图像应用一组转换的结果。这些变体用于从原始图像创建缩略图、固定大小的头像或任何其他派生图像。

变体依赖于 ImageProcessing Gem 来实际转换文件,因此如果您希望使用变体,您必须将 gem "image_processing" 添加到您的 Gemfile 中。默认情况下,图像将使用 ImageMagickMiniMagick Gem 进行处理,但您也可以切换到由 ruby-vips Gem 操作的 libvips 处理器。

Rails.application.config.active_storage.variant_processor
# => :mini_magick

Rails.application.config.active_storage.variant_processor = :vips
# => :vips

请注意,要创建变体,有必要从服务下载整个 Blob 文件。由于此过程,您还需要考虑何时实际处理变体。例如,您不应在模板中内联处理变体。将处理延迟到按需控制器,例如 ActiveStorage::RepresentationsController 中提供的控制器。

要引用此类延迟的按需变体,只需通过 Active Storage 提供的已解析路由链接到变体,如下所示

<%= image_tag Current.user.avatar.variant(resize_to_limit: [100, 100]) %>

这将为具有该特定变体的特定 Blob 创建一个 URL,然后 ActiveStorage::RepresentationsController 可以按需生成该 URL。

当您确实想要生成所需的变体时,请调用 processed。这将检查变体是否已处理并上传到服务,如果是,则只返回该变体。否则,它将执行转换,将变体上传到服务,然后再次返回自身。示例

avatar.variant(resize_to_limit: [100, 100]).processed.url

这将创建并处理头像 Blob 的变体,该变体的约束高度和宽度为 100。然后,它会根据 Blob 的派生键和转换将所述变体上传到服务。

您可以将任意数量的 ImageMagick/libvips 操作组合到变体中,以及 ImageProcessing Gem 提供的任何宏(例如 resize_to_limit

avatar.variant(resize_to_limit: [800, 800], colourspace: "b-w", rotate: "-90")

访问以下链接以获取可用 ImageProcessing 命令和 ImageMagick/libvips 操作的列表

方法
D
F
I
K
N
P
U

属性

[R] blob
[R] variation

类公共方法

new(blob, variation_or_variation_key)

# File activestorage/app/models/active_storage/variant.rb, line 62
def initialize(blob, variation_or_variation_key)
  @blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key)
end

实例公共方法

destroy()

从服务中删除变体文件。

# File activestorage/app/models/active_storage/variant.rb, line 102
def destroy
  service.delete(key)
end

download(&block)

下载与此变体关联的文件。如果未给定块,则将整个文件读入内存并返回。对于非常大的文件,这将使用大量 RAM。如果给定一个块,则下载将被流化并分块生成。

# File activestorage/app/models/active_storage/variant.rb, line 88
def download(&block)
  service.download key, &block
end

filename()

# File activestorage/app/models/active_storage/variant.rb, line 92
def filename
  ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format.downcase}"
end

image()

返回接收变体。允许 ActiveStorage::VariantActiveStorage::Preview 实例可以互换使用。

# File activestorage/app/models/active_storage/variant.rb, line 97
def image
  self
end

key()

返回 blob 和变体的组合键,它们共同标识一个特定变体。

# File activestorage/app/models/active_storage/variant.rb, line 73
def key
  "variants/#{blob.key}/#{OpenSSL::Digest::SHA256.hexdigest(variation.key)}"
end

processed()

在变体实例经过处理或在服务中找到现有处理后,返回变体实例本身。

# File activestorage/app/models/active_storage/variant.rb, line 67
def processed
  process unless processed?
  self
end

url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline)

返回服务上 blob 变体的 URL。有关详细信息,请参阅 {ActiveStorage::Blob#url}。

使用 url_for(variant)(或隐含形式,如 link_to variantredirect_to variant)获取指向 ActiveStorage::RepresentationsController 的变体的稳定 URL,而后者将使用此 service_call 方法进行重定向。

# File activestorage/app/models/active_storage/variant.rb, line 82
def url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline)
  service.url key, expires_in: expires_in, disposition: disposition, filename: filename, content_type: content_type
end