Active Storage 变体¶ ↑
Image blobs can have variants that are the result of a set of transformations applied to the original. These variants are used to create thumbnails, fixed-size avatars, or any other derivative image from the original. (图片 blob 可以有变体,这些变体是对原始图片应用一系列转换的结果。这些变体用于创建缩略图、固定大小的头像或原始图片的任何其他衍生图片。)
Variants rely on ImageProcessing gem for the actual transformations of the file, so you must add gem "image_processing" to your Gemfile if you wish to use variants. By default, images will be processed with libvips using the ruby-vips gem, but you can also switch to the ImageMagick processor operated by the MiniMagick gem). (变体依赖 ImageProcessing gem 来实际转换文件,因此如果您想使用变体,必须在 Gemfile 中添加 gem "image_processing"。默认情况下,图片将使用 ruby-vips gem 通过 libvips 进行处理,但您也可以切换到由 MiniMagick gem 操作的 ImageMagick 处理器。)
Rails.application.config.active_storage.variant_processor # => :vips Rails.application.config.active_storage.variant_processor = :mini_magick # => :mini_magick
Note that to create a variant it’s necessary to download the entire blob file from the service. Because of this process, you also want to be considerate about when the variant is actually processed. You shouldn’t be processing variants inline in a template, for example. Delay the processing to an on-demand controller, like the one provided in ActiveStorage::Representations::ProxyController and ActiveStorage::Representations::RedirectController. (请注意,创建变体需要从服务中下载整个 blob 文件。由于这个过程,您还需要考虑何时实际处理变体。例如,您不应该在模板中内联处理变体。将处理延迟到按需控制器,例如 ActiveStorage::Representations::ProxyController 和 ActiveStorage::Representations::RedirectController 中提供的控制器。)
To refer to such a delayed on-demand variant, simply link to the variant through the resolved route provided by Active Storage like so (要引用这种延迟的按需变体,只需像这样通过 Active Storage 提供的解析路由链接到该变体:)
<%= image_tag Current.user.avatar.variant(resize_to_limit: [100, 100]) %>
This will create a URL for that specific blob with that specific variant, which the ActiveStorage::Representations::ProxyController or ActiveStorage::Representations::RedirectController can then produce on-demand. (这将为具有该特定变体的该特定 blob 创建一个 URL,然后 ActiveStorage::Representations::ProxyController 或 ActiveStorage::Representations::RedirectController 可以按需生成该 URL。)
When you do want to actually produce the variant needed, call processed. This will check that the variant has already been processed and uploaded to the service, and, if so, just return that. Otherwise it will perform the transformations, upload the variant to the service, and return itself again. Example (当您确实想实际生成所需的变体时,请调用 processed。这将检查变体是否已处理并上传到服务,如果已处理,则直接返回。否则,它将执行转换,将变体上传到服务,然后再次返回自身。示例:)
avatar.variant(resize_to_limit: [100, 100]).processed.url
This will create and process a variant of the avatar blob that’s constrained to a height and width of 100. Then it’ll upload said variant to the service according to a derivative key of the blob and the transformations. (这将创建一个头像 blob 的变体并对其进行处理,该变体的宽度和高度限制为 100。然后,它将根据 blob 的衍生键和转换将该变体上传到服务。)
You can combine any number of ImageMagick/libvips operations into a variant, as well as any macros provided by the ImageProcessing gem (such as resize_to_limit) (您可以将任意数量的 ImageMagick/libvips 操作合并到一个变体中,以及 ImageProcessing gem 提供的任何宏(例如 resize_to_limit)。)
avatar.variant(resize_to_limit: [800, 800], colourspace: "b-w", rotate: "-90")
Visit the following links for a list of available ImageProcessing commands and ImageMagick/libvips operations (请访问以下链接以获取可用的 ImageProcessing 命令以及 ImageMagick/libvips 操作列表:)
- D
- F
- I
- K
- N
- P
- U
Attributes
| [R] | blob | |
| [R] | 变体 (variation) |
类公共方法
new(blob, variation_or_variation_key) 链接
Source: 显示 | 在 GitHub 上
# 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() 链接
Deletes variant file from service. (从服务中删除变体文件。)
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 102 def destroy service.delete(key) end
download(&block) 链接
Downloads the file associated with this variant. If no block is given, the entire file is read into memory and returned. That’ll use a lot of RAM for very large files. If a block is given, then the download is streamed and yielded in chunks. (下载与此变体关联的文件。如果未提供块,则将整个文件读入内存并返回。对于非常大的文件,这会占用大量内存。如果提供了块,则下载将被流式传输并在块中给出。)
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 88 def download(&block) service.download key, &block end
filename() 链接
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 92 def filename ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format.downcase}" end
image() 链接
Returns the receiving variant. Allows ActiveStorage::Variant and ActiveStorage::Preview instances to be used interchangeably. (返回接收的变体。允许 ActiveStorage::Variant 和 ActiveStorage::Preview 实例互换使用。)
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 97 def image self end
key() 链接
Returns a combination key of the blob and the variation that together identifies a specific variant. (返回 blob 和变体的组合键,它们共同标识一个特定的变体。)
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 73 def key "variants/#{blob.key}/#{OpenSSL::Digest::SHA256.hexdigest(variation.key)}" end
processed() 链接
Returns the variant instance itself after it’s been processed or an existing processing has been found on the service. (在变体实例处理完毕或在服务中找到现有处理后,返回该变体实例本身。)
Source: 显示 | 在 GitHub 上
# 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) 链接
Returns the URL of the blob variant on the service. See ActiveStorage::Blob#url for details. (返回服务上 blob 变体的 URL。有关详细信息,请参阅 ActiveStorage::Blob#url。)
Use url_for(variant) (or the implied form, like link_to variant or redirect_to variant) to get the stable URL for a variant that points to the ActiveStorage::Representations::ProxyController or ActiveStorage::Representations::RedirectController, which in turn will use this service_call method for its redirection. (使用 url_for(variant)(或隐含形式,如 link_to variant 或 redirect_to variant)获取指向 ActiveStorage::Representations::ProxyController 或 ActiveStorage::Representations::RedirectController 的变体的稳定 URL,然后该控制器将使用此 service_call 方法进行重定向。)
Source: 显示 | 在 GitHub 上
# 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