- P
- R
- V
實例公開方法
預覽(轉換) 鏈結
傳回一個 ActiveStorage::Preview
實例,其中包含提供的 轉換
集合。 預覽是從非影像 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, "No previewer found for blob with ID=#{id} and content_type=#{content_type}" end end
可預覽?() 鏈結
如果任一已註冊預覽器接受該 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
可表示?() 鏈結
如果 Blob 是變數或可預覽的,則傳回 true。
來源:顯示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 97 def representable? variable? || previewable? end
表示(轉換) 鏈結
針對可預覽的 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, "No previewer found and can't transform blob with ID=#{id} and content_type=#{content_type}" end end
變數?() 鏈結
如果變形處理器可以轉換 Blob(其內容類型在 ActiveStorage.variable_content_types
)。
來源:顯示 | 在 GitHub 上
# File activestorage/app/models/active_storage/blob/representable.rb, line 44 def variable? ActiveStorage.variable_content_types.include?(content_type) end
變形(變形)連結
傳回具有所提供的 變形
集合的 ActiveStorage::Variant
或 ActiveStorage::VariantWithRecord
執行個體。這僅與影像檔相關,而且允許使用者將任何影像轉換大小、顏色等。範例
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, "Can't transform blob with ID=#{id} and content_type=#{content_type}" end end