Active Storage 變式
影像 blob 可有變式,這是對原件套用一組轉換的結果。這些變式用於建立縮圖、固定大小大頭貼或任何從原件產生的衍生影像。
變式倚賴 ImageProcessing gem 來執行檔案的實際轉換,因此如果想使用變式,您必須在 Gemfile 中新增 gem "image_processing"
。預設情況下,影像會使用 ImageMagick 來處理,並使用 MiniMagick gem,但您也可以切換到 libvips 處理器,它由 ruby-vips gem 執行。
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 建立網址,這個特定變式,然後 ActiveStorage::RepresentationsController 可以按需產生。
當您真的想要產生需要的變式時,請呼叫 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 作業清單
屬性
[R] | blob | |
[R] | 變異 |
類別公用方法
new(blob, variation_or_variation_key) 連結
來源: 顯示 | 在 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
執行個體公用方法
銷毀() 連結
從服務中刪除變式檔案。
來源: 顯示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 102 def destroy service.delete(key) end
下載(&block) 連結
下載與此變體相關聯的文件。如果沒有給定區段,整個文件將讀取至記憶體中並回傳。對於非常大的文件,可能會使用大量 RAM。如果指定了區段,下載會透過串流傳輸,並漸次產出區段。
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() 連結
傳回接收變體。允許 ActiveStorage::Variant
和 ActiveStorage::Preview
執行個體可以互換使用。
Source: 顯示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variant.rb, line 97 def image self end
key() 連結
傳回 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() 連結
在處理過後傳回變異執行個體,或是在服務中找到現有的處理作業。
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) 連結
傳回 blob 變異在服務上的 URL。詳情請參閱 {ActiveStorage::Blob#url}。
使用 url_for(變異)
(或暗示形式,例如 link_to 變異
或 redirect_to 變異
) 來取得指向 ActiveStorage::RepresentationsController 的變異穩定 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