Using Media Assets on Products
The commercetools platform offers the common type Asset that is embedded in other endpoints like the Products. To follow this tutorial, you should have a project set up, as described in Getting Started, and have a product onto which you want to add assets.
Adding an asset
Let's add a simple asset, a PDF, to the product variant:
POST /{projectKey}/products/<product-id>
with:
final AssetSource assetSource = AssetSourceBuilder.ofUri("http://example.org/content/product-manual.pdf").contentType("application/pdf").build();final AssetDraft assetDraft = AssetDraftBuilder.of(asList(assetSource), en("Product Manual")).description(en("The manual for my product.")).tags(asSet("manual", "pdf")).build();final AddAsset addAsset = AddAsset.ofVariantId(1, assetDraft);final ProductUpdateCommand productUpdateCommand = ProductUpdateCommand.of(product, addAsset);return client.execute(productUpdateCommand);
We've set a name
and a description
. We've also added tags
and set the optional contentType
on the source. Both can be used by your frontend to decide how to display the asset, or search for a specific one in the assets
array. For example, if there is a Manual-Section on your product details page, you can search for all assets that have the tag manual, and based on the contentType
decide whether to show the content in a PDF viewer or a video player.
Using Asset Sources
An AssetSource
is a representation of an Asset
in a specific format, for example a video in a certain encoding, or an image in a certain resolution. To pick the right source, you can optionally supply a key
or the dimensions of the asset.
Using Asset Sources for offering an image in different resolutions
In this example, the image is available in three resolutions: One is a thumb to be displayed on a product overview page, one is a regular resolution to be displayed on a product details page, and one is a high-res version to be used when the customer zooms into the image.
final AssetSource sourceProductFull = AssetSourceBuilder.ofUri("http://example.org/product-full.jpg").dimensions(AssetDimensions.ofWidthAndHeight(600, 400)).key("full").build();final AssetSource sourceProductThumb = AssetSourceBuilder.ofUri("http://example.org/product-thumb.jpg").dimensions(AssetDimensions.ofWidthAndHeight(90, 60)).key("thumb").build();final AssetSource sourceProductZoom = AssetSourceBuilder.ofUri("http://example.org/product-zoom.jpg").dimensions(AssetDimensions.ofWidthAndHeight(3000, 2000)).key("zoom").build();final AssetDraft assetDraft = AssetDraftBuilder.of(asList(sourceProductFull, sourceProductThumb, sourceProductZoom), en("My Product Image")).build();
In this example, both the key
and the dimensions
are set. The frontend may use the key
to find the thumbnail by finding the source for which the key is "thumb"
. Alternatively, one could save many resolutions, and the frontend could figure out which image is best used (for example a thumbnail on a retina-display would use a different resolution than on a regular display).
You can also use the dimensions
field to generate the HTML srcset
property:
<imgsrc="https://example.org/product-full.jpg"srcset="https://example.org/product-thumb.jpg 90w,https://example.org/product-full.jpg 600w,https://example.org/product-zoom.jpg 3000w"/>
Using Asset Sources for offering a video in different encodings
In this example, the video is encoded for in MP4, WebM, and HTTP Live Streaming (HLS). A thumbnail is also available.
{"assetId": "<id>","name": { "en": "My Product Video" },"sources": [{"uri": "https://example.org/product-video.mp4","key": "flash","contentType": "video/mp4"},{"uri": "https://example.org/product-video.webm","key": "html5","contentType": "video/webm"},{"uri": "https://example.org/product-video.m3u8","key": "mobile","contentType": "application/x-mpegURL"},{"uri": "https://example.org/product-video-thumb.jpg","key": "thumb","contentType": "application/jpeg"}]}
The flash player or a mobile app could look for its file by the key
. In HTML5, the video
element supports multiple sources and the browser will pick the most suitable source:
<video controls poster="https://example.org/product-video-thumb.jpg"><source src="https://example.org/product-video.mp4" type="video/mp4" /><source src="https://example.org/product-video.webm" type="video/webm" /><sourcesrc="https://example.org/product-video.m3u8"type="application/x-mpegURL"/></video>
Conclusion
You've seen how to create a simple asset like a PDF, how a frontend can select the right asset out of multiple ones based on tags, and how asset sources can be used to offer different image resolutions or video encodings.