影像是 發展豐富而富有吸引力AEM的體驗。 無AEM頭支援映像資產的管理及其優化交付。
用於無頭內容建AEM模的內容片段,通常引用影像資源,以便在無頭體驗中顯示。 可AEM以編寫GraphQL查詢,以根據引用影像的位置為影像提供URL。
的 ImageRef
類型具有三個內容引用的URL選項:
_path
是中引用的路AEM徑,且不包AEM括源(主機名)_authorUrl
是AEM作者上影像資產的完整URL
_publishUrl
是AEM發佈上影像資產的完整URL
根據以下條件,最好使用這些欄位:
ImageRef欄位 | 客戶端Web應用從AEM | 客戶端應用查詢AEM作者 | 客戶端應用查詢AEM發佈 |
---|---|---|---|
_path |
✔ | ✘ | ✘ |
_authorUrl |
✘ | ✔ | ✘ |
_publishUrl |
✘ | ✘ | ✔ |
使用 _authorUrl
和 _publishUrl
應與用AEM於源GraphQL響應的GraphQL終結點對齊。
確保包含影像引用的「內容片段」欄位是 內容引用 資料類型。
在 內容片段模型,通過選擇欄位並檢查 屬性 的上界。
在GraphQL查詢中,將欄位返回為 ImageRef
類型,並請求相應的欄位 _path
。 _authorUrl
或 _publishUrl
應用程式所需的。
{
adventureByPath(_path: "/content/dam/wknd/en/adventures/bali-surf-camp/bali-surf-camp") {
item {
adventurePrimaryImage {
... on ImageRef {
_path,
_authorUrl,
_publishUrl
}
}
}
}
}
生成的JSON響應包含包含映像資產的URls的請求欄位。
{
"data": {
"adventureByPath": {
"item": {
"adventurePrimaryImage": {
"_path": "/content/dam/wknd/en/adventures/bali-surf-camp/AdobeStock_175749320.jpg",
"_authorUrl": "https://author-p123-e456.adobeaemcloud.com/content/dam/wknd/en/adventures/bali-surf-camp/AdobeStock_175749320.jpg",
"_publishUrl": "https://publish-p123-e789.adobeaemcloud.com/content/dam/wknd/en/adventures/bali-surf-camp/AdobeStock_175749320.jpg"
}
}
}
}
}
要在應用程式中載入引用的映像,請使用相應的欄位, _path
。 _authorUrl
或 _publishUrl
的 adventurePrimaryImage
作為影像的源URL。
的域 _authorUrl
和 _publishUrl
由AEMas a Cloud Service使用 外部化器。
在React中,顯示AEM Publish中的影像如下所示:
<img src={ data.adventureByPath.item.adventurePrimaryImage._publishUrl } />
映像資產支援可定製 格式,是原始資產的替代表。 自定義格式副本有助於優化無頭體驗。 與請求原始影像資產(通常是大型高解析度檔案)不同,優化的格式副本可由無頭應用程式請求。
AEM Assets管理員使用「處理配置檔案」定義自定義格式副本。 然後,「處理配置檔案」可以直接應用於特定資料夾樹或資產,以便為這些資產生成格式副本。
資產格式副本規範在中定義 處理配置檔案 由AEM Assets管理人負責。
建立或更新「處理配置檔案」,並添加無頭應用程式所需的影像大小的格式副本定義。 格式副本可以命名為任何內容,但應在語義上命名。
在此示例中,將建立三個格式副本:
格式副本名稱 | 副檔名 | 最大寬度 |
---|---|---|
大 | jpeg | 1200px |
中 | jpeg | 900px |
小 | jpeg | 600px |
上表中調用的屬性非常重要:
格式副本定義取決於無頭應用程式的需要,因此請確保為使用案例定義最佳格式副本集,並在使用方式方面以語義命名。
建立(或更新)「處理配置檔案」後,重新處理資產以生成在「處理配置檔案」中定義的新格式副本。 如果資產未與新格式副本一起處理,則新格式副本將不存在。
最好, 已將「處理配置檔案」分配給資料夾 因此,任何新資產都會自動生成格式副本。 現有資產必須採用下面的臨時辦法重新處理。
或者,通過選擇資料夾或資產,選擇 重新處理資產,然後選擇新的「處理配置檔案」名稱。
格式副本可通過 開啟資產的格式副本視圖,並在格式副本欄中選擇新格式副本以進行預覽。 如果缺少格式副本, 確保使用處理配置檔案處理資產。
確保具有新格式副本的資產 (重新發佈) 因此,新格式副本可在AEM Publish(AEM發佈)上訪問。
通過附加 格式副本名稱 和 格式副本擴展 在「處理配置檔案」中定義到資產的URL。
資產網址 | 格式副本子路徑 | 格式副本名稱 | 格式副本擴展 | 格式副本URL | |
---|---|---|---|---|---|
https://publish-p123-e789.adobeaemcloud.com/content/dam/example.jpg | /_jcr內容/格式副本/ | 大 | .jpeg | → | https://publish-p123-e789.adobeaemcloud.com/content/dam/example.jpg/_jcr_content/renditions/large.jpeg |
https://publish-p123-e789.adobeaemcloud.com/content/dam/example.jpg | /_jcr內容/格式副本/ | 中 | .jpeg | → | https://publish-p123-e789.adobeaemcloud.com/content/dam/example.jpg/_jcr_content/renditions/medium.jpeg |
https://publish-p123-e789.adobeaemcloud.com/content/dam/example.jpg | /_jcr內容/格式副本/ | 小 | .jpeg | → | https://publish-p123-e789.adobeaemcloud.com/content/dam/example.jpg/_jcr_content/renditions/small.jpeg |
AEMGraphQL確實需要額外語法來請求影像格式副本。 但 影像查詢 以常規方式,並以代碼形式指定所需的格式副本。 重要的是 確保無頭應用程式使用的影像資產具有同名格式副本。
讓我們建立一個簡單的「反應」應用程式,該應用程式顯示單個影像資產的三個格式副本:小、中和大。
建立呈現影像的「反應」元件。 此元件接受四個屬性:
assetUrl
:通過GraphQL查詢的響應提供的影像資產URL。renditionName
:要載入的格式副本的名稱。renditionExtension
:要載入的格式副本的擴展。alt
:影像的Alt文本;輔助功能非常重要!此元件構建 格式副本URL,使用中概述的格式 訪問格式副本。 安 onError
處理程式設定為在格式副本丟失時顯示原始資產。
此示例使用原始資產url作為 onError
處理程式,在該事件中缺少格式副本。
// src/Image.js
export default function Image({ assetUrl, renditionName, renditionExtension, alt }) {
// Construct the rendition Url in the format:
// <ASSET URL>/_jcr_content/renditions<RENDITION NAME>.<RENDITION EXTENSION>
const renditionUrl = `${assetUrl}/_jcr_content/renditions/${renditionName}.${renditionExtension}`;
// Load the original image asset in the event the named rendition is missing
const handleOnError = (e) => { e.target.src = assetUrl; }
return (
<>
<img src={renditionUrl}
alt={alt}
onError={handleOnError}/>
</>
);
}
App.js
這個簡單 App.js
查AEM詢冒險影像,然後顯示該影像的三個格式副本:小、中和大。
在自定AEM義React掛接中執行查詢 使用Headless SDKAEM的GraphQL。
查詢結果和特定格式副本參數將傳遞給 影像反應元件。
// src/App.js
import "./App.css";
import { useGraphQL } from "./useGraphQL";
import Image from "./Image";
function App() {
// The GraphQL that returns an image
const adventureQuery = `{
adventureByPath(_path: "/content/dam/wknd/en/adventures/bali-surf-camp/bali-surf-camp") {
item {
adventureTitle,
adventurePrimaryImage {
... on ImageRef {
_path,
_authorUrl,
_publishUrl
}
}
}
}
}`;
// Get data from AEM using GraphQL
let { data } = useGraphQL(adventureQuery);
// Wait for GraphQL to provide data
if (!data) { return <></> }
return (
<div className="app">
<h2>Small rendition</h2>
{/* Render the small rendition for the Adventure Primary Image */}
<Image
assetUrl={data.adventureByPath.item.adventurePrimaryImage._publishUrl}
renditionName="small"
renditionExtension="jpeg"
alt={data.adventureByPath.item.adventureTitle}
/>
<hr />
<h2>Medium rendition</h2>
{/* Render the medium rendition for the Adventure Primary Image */}
<Image
assetUrl={data.adventureByPath.item.adventurePrimaryImage._publishUrl}
renditionName="medium"
renditionExtension="jpeg"
alt={data.adventureByPath.item.adventureTitle}
/>
<hr />
<h2>Large rendition</h2>
{/* Render the large rendition for the Adventure Primary Image */}
<Image
assetUrl={data.adventureByPath.item.adventurePrimaryImage._publishUrl}
renditionName="large"
renditionExtension="jpeg"
alt={data.adventureByPath.item.adventureTitle}
/>
</div>
);
}
export default App;