Media (OrchardCore.Media
)¶
The Media module provides a UI to upload and organize binary files that can be used while creating content.
The media-processing liquid filters can also create custom sized images.
HTML filters¶
The following filters allow for media manipulation:
asset_url
¶
Returns the URL of a media file, based on its location in the media library.
Input¶
{{ 'animals/kittens.jpg' | asset_url }}
or when using your added content
{{ Model.ContentItem.Content.YourContentType.YourMediaField.Paths.first | asset_url }}
Output¶
/media/animals/kittens.jpg
img_tag
¶
Renders an <img src />
HTML tag.
img_tag Input¶
{{ 'animals/kittens.jpg' | asset_url | img_tag }}
img_tag Output¶
<img src="~/media/animals/kittens.jpg" />
Options¶
You can add as many html attributes as you want with the img_tag.
{{ 'animals/kittens.jpg' | asset_url | img_tag: alt: 'kittens', class: 'kittens black', data_order: some_var }}
Image resizing filters¶
resize_url
¶
Convert the input URL to create a resized image with the specified size arguments.
resize_url Input¶
{{ 'animals/kittens.jpg' | asset_url | resize_url: width:100, height:240 | img_tag }}
resize_url Output¶
<img src="~/media/animals/kittens.jpg?width=100&height=240" />
Arguments¶
Refer Query string tokens to understand the valid values for a width or height command, and when a query string must utilize a token.
Note
You cannot mix named and indexed arguments. If any of the arguments is named, all arguments must be named.
width
(or first argument)¶
The width of the new image. One of the allowed values.
height
(or second argument)¶
The height of the new image. One of the allowed values.
mode
(or third argument)¶
The resize mode.
pad
¶
Pads the resized image to fit the bounds of its container.
If only one dimension is passed, the original aspect ratio will be maintained.
boxpad
¶
Pads the image to fit the bounds of the container without resizing the original source.
When downscaling, performs the same functionality as pad
.
max
(Default)¶
Constrains the resized image to fit the bounds of its container maintaining the original aspect ratio.
min
¶
Resizes the image until the shortest side reaches the given dimension. Upscaling is disabled in this mode and the original image will be returned if attempted.
stretch
¶
Stretches the resized image to fit the bounds of its container.
crop
¶
Resizes the image using the same functionality as max
then removes any image area falling outside the bounds of its container.
mode Input¶
{{ 'animals/kittens.jpg' | asset_url | resize_url: width:100, height:240, mode:'crop' }}
mode Output¶
<img src="~/media/animals/kittens.jpg?width=100&height=240&rmode=crop" />
quality
(or fourth argument)¶
The quality used when compressing the image.
Note
The quality argument is only supported for JPG
images, but can be combined with the format
argument to convert to JPG
format
(or fifth argument)¶
The image format to use when processing the output of an image.
Supported formats include bmp
, gif
, jpg
, png
, tga
.
Can be combined with the quality
argument to convert an image to a JPG
and reduce the quality.
quality/format Input¶
{{ 'animals/kittens.jpg' | asset_url | resize_url: width:100, height:240, mode:'crop', quality: 50, format:'jpg' }}
quality/format Output¶
<img src="~/media/animals/kittens.jpg?width=100&height=240&rmode=crop&quality=50&format=jpg" />
anchor
(or sixth argument)¶
The anchor of the new image.
anchor Input¶
{% assign anchor = Model.ContentItem.Content.Blog.Image.Anchors.first %}
{{ 'animals/kittens.jpg' | asset_url | resize_url: width:100, height:240, mode:'crop', anchor:anchor }}
anchor Output¶
<img src="~/media/animals/kittens.jpg?width=100&height=240&rmode=crop&rxy=0.5,0.5" />
bgcolor
(or seventh argument)¶
The background color of the new image when mode
is pad
or boxpad
. Examples of valid values: white
, ffff00
, ffff0080
, 128,64,32
and 128,64,32,16
.
bgcolor
Input¶
{{ 'animals/kittens.jpg' | asset_url | resize_url: width:100, height:240, mode:'pad', bgcolor:'white' }}
bgcolor
Output¶
<img src="~/media/animals/kittens.jpg?width=100&height=240&rmode=pad&bgcolor=white" />
profile
(named argument)¶
A Media Profile can be specified as a named argument to provide preset formatting commands.
profile
Input¶
{{ 'animals/kittens.jpg' | asset_url | resize_url: profile : 'medium' }}
profile
Output¶
<img src="~/media/animals/kittens.jpg?width=240&height=240" />
append_version
¶
Appends a version hash for an asset. Can be piped together with the other media filters.
version Input¶
{{ 'animals/kittens.jpg' | asset_url | append_version | img_tag }}
version Output¶
<img src="~/media/animals/kittens.jpg?v=Ailxbj_jQtYc9LRXKa21DygRzmQqc3OfN1XxSaQ3UWE" />
Razor Helpers¶
To obtain the correct URL for an asset, use the AssetUrl
helper extension method on the view's base Orchard
property, e.g.:
@Orchard.AssetUrl(Model.Paths[0])
To obtain the correct URL for a resized asset use AssetUrl
with the optional width, height and resizeMode parameters, e.g.:
@Orchard.AssetUrl(Model.Paths[0], width: 100 , height: 240, resizeMode: ResizeMode.Crop)
To obtain the correct URL for a resized asset use AssetUrl
with the optional width, height, resizeMode, quality and format parameters, e.g.:
@Orchard.AssetUrl(Model.Paths[0], width: 100 , height: 240, resizeMode: ResizeMode.Crop, quality: 50, format: Format.Jpg)
To obtain the correct URL for a resized asset use AssetUrl
with the optional width, height, resizeMode and bgcolor, e.g.:
@Orchard.AssetUrl(Model.Paths[0], width: 100 , height: 240, resizeMode: ResizeMode.Pad, bgcolor: "white")
To append a version hash for an asset use AssetUrl
with the append version parameter, e.g.:
@Orchard.AssetUrl(Model.Paths[0], appendVersion: true)
or with resizing options as well, noting that the version hash is based on the source image
@Orchard.AssetUrl(Model.Paths[0], width: 100 , height: 240, resizeMode: ResizeMode.Crop, appendVersion: true)
To use a Media Profile, use the AssetProfileUrlAsync
helper extension method on the view's base Orchard
property, e.g.:
@await Orchard.AssetProfileUrlAsync(Model.Paths[0], "medium")
To use Image Anchors, use the GetAnchors
helper extension method on the media field, e.g.:
@await Orchard.AssetUrl(Model.Paths[0], , width: 100 , height: 240, resizeMode: ResizeMode.Crop, @Model.Field.GetAnchors()[0])
Razor image resizing tag helpers¶
To use the image tag helpers add @addTagHelper *, OrchardCore.Media
to _ViewImports.cshtml
, and take a direct reference to the OrchardCore.Media
nuget package.
asset-src
is used to obtain the correct URL for the asset and set the src
attribute. Width, height, resize mode, quality and format can be set using img-width
, img-height
, img-resize-mode
, img-quality
, and img-format
respectively. e.g.:
<img asset-src="Model.Paths[0]" alt="..." img-width="100" img-height="240" img-resize-mode="Crop" img-quality="50" img-format="Jpg" />
Alternatively the Asset Url can be resolved independently and the src
attribute used:
<img src="@Orchard.AssetUrl(Model.Paths[0])" alt="..." img-width="100" img-height="240" img-resize-mode="Crop" img-quality="50" img-format="Jpg" />
When resize mode is pad
or boxpad
, the background color can be set using img-bgcolor
, e.g.:
<img asset-src="Model.Paths[0]" alt="..." img-width="100" img-height="240" img-resize-mode="Pad" img-bgcolor="white" />
To use a Media Profile set the asset-src
property and the img-profile
attribute.
<img asset-src="Model.Paths[0]" alt="..." img-profile="medium" />
You can optionally include more formatting information, or override the profiles properties.
<img asset-src="Model.Paths[0]" alt="..." img-profile="medium" img-quality="50" img-format="Jpg" />
To use a Media Text set the alt
attribute.
<img asset-src="Model.Paths[0]" alt="@Model.MediaTexts[0]" />
To use a Image Anchor set the asset-src
property and the img-anchor
attribute.
<img asset-src="Model.Paths[0]" alt="..." img-width="100" img-height="240" img-profile="medium" img-anchor="@Model.GetAnchors()[0]" />
Razor append version¶
asp-append-version
support is available on the OrchardCore tag helpers and MVC tag helpers.
<img asset-src="Model.Paths[0]" alt="..." asp-append-version="true" />
Alternatively the Asset Url can be resolved independently and the src
attribute used:
<img src="@Orchard.AssetUrl(Model.Paths[0])" alt="..." asp-append-version="true" />
Or when using the MVC tag helpers and the image is resolved from static assets, i.e. wwwroot
<img src="/favicon.ico" asp-append-version="true"/>
The Razor Helper is accessible on the
Orchard
property if the view is using Orchard Core's Razor base class, or by injectingOrchardCore.IOrchardHelper
in all other cases.
Note
When using tag helpers in Razor, you must take a direct reference to the OrchardCore.Media
nuget package in each theme or module that uses the tag helpers. This is not required when using Liquid.
Deployment Step Editor¶
Keep these things in mind when working with the deployment step editor:
- Selecting "Include all media." will ensure that all media is added to the package when this deployment plan executes, regardless of what you see here now.
- Selecting a file will ensure that only that file is added to the package when this deployment plan executes, regardless of what you see here now.
- Selecting a directory will ensure that all the files in that directory at the time this deployment plan executes, are added to the package during execution, regardless of what you see here now.
- Selecting all files in a directory will ensure that only those files are added to the package when this deployment plan executes, even if at that time, that directory has more files than what you see here now.
Configuration¶
The following configuration values are used by default and can be customized:
"OrchardCore_Media": {
// The accepted sizes for custom width and height.
// When the 'UseTokenizedQueryString' is True (default) all sizes are valid.
"SupportedSizes": [ 16, 32, 50, 100, 160, 240, 480, 600, 1024, 2048 ],
// The number of days to store images in the browser cache.
// NB: To control cache headers for module static assets, refer to the Orchard Core Modules Section.
"MaxBrowserCacheDays": 30,
// The number of days to store secure media files in the browser cache.
// Set to 0 (default) to disable caching secure files.
"MaxSecureFilesBrowserCacheDays": 0,
// The number of days a cached resized media item will be valid for, before being rebuilt on request.
"MaxCacheDays": 365,
// The maximum size of an uploaded file in bytes.
// NB: You might still need to configure the limit in IIS (https://docs.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/requestlimits/)
"MaxFileSize": 30000000,
// A CDN base url that will be prefixed to the request path when serving images.
"CdnBaseUrl": "https://your-cdn.com",
// The path used when serving media assets.
"AssetsRequestPath": "/media",
// The name of the folder used to store media assets inside the App_Data folder.
"AssetsPath": "Media",
// Whether to use a token in the query string to prevent disc filling.
"UseTokenizedQueryString": true,
// The list of allowed file extensions
"AllowedFileExtensions": [
// Images
".jpg",
".jpeg",
".png",
".gif",
".ico",
".svg",
// Documents
".pdf", // Portable Document Format; Adobe Acrobat
".doc", // Microsoft Word Document
".docx",
".ppt", // Microsoft PowerPoint Presentation
".pptx",
".pps",
".ppsx",
".odt", // OpenDocument Text Document
".xls", // Microsoft Excel Document
".xlsx",
".psd", // Adobe Photoshop Document
// Audio
".mp3",
".m4a",
".ogg",
".wav",
// Video
".mp4", // MPEG-4
".m4v",
".mov", // QuickTime
".wmv", // Windows Media Video
".avi",
".mpg",
".ogv", // Ogg
".3gp", // 3GPP
],
// The Content Security Policy to apply to assets served from the media library.
"ContentSecurityPolicy" : "default-src 'self'; style-src 'unsafe-inline'",
// The maximum chunk size when uploading files in bytes. If 0, no chunked upload is used. This is useful to work around request size limitations of a hosting environment.
"MaxUploadChunkSize": 104857600,
// The lifetime of temporary files created during upload. Defaults to 1 hour.
"TemporaryFileLifetime": "01:00:00"
}
To configure the StaticFileOptions
in more detail, including event handlers, for the Media Library StaticFileMiddleware
apply:
services.PostConfigure<MediaOptions>(o => ...);
To configure the ImageSharpMiddleware
in more detail, including event handlers, apply:
services.PostConfigure<ImageSharpMiddlewareOptions>(o => ...);
Note
The Media Library StaticFileOptions
configuration is separated from the configuration for static files contained in module wwwroot
folders.
To configure wwwroot
static file options apply:
services.Configure<StaticFileOptions>(o => ...);
Media Profiles¶
Media profiles allow you to defined preset image resizing and formatting commands.
You can create a media profile from the Configuration -> Media -> Media Profiles menu.
When specifying a media profile with either the liquid, razor helper, or tag helper you provide the profile name, and any additional commands which you want to apply to the media item.
{% resize_url profile: 'medium' %}
{% resize_url profile: 'medium', mode: 'crop' %}
@await Orchard.AssetProfileUrlAsync(Model.Paths[0], "medium");
@await Orchard.AssetProfileUrlAsync(Model.Paths[0], "medium", resizeMode: ResizeMode.Crop);
<img asset-src="Model.Paths[0]" img-profile="medium" />
<img asset-src="Model.Paths[0]" img-profile="medium" img-resize-mode="Crop"/>
Media Text¶
Media text is an optional setting, on by default, on the MediaField
.
When provided it allows the editor of the field to include a text value for each selected media item.
This can be used for the alt
tag of an image.
When the setting is enabled the template must read and provide the value to the img
tag.
The MediaTexts[]
is kept in sync with the Paths[]
array and the index for a given path represents the index of a MediaText
value.
Image Anchors¶
Image anchors are an optional setting, off by default, on the MediaField
.
When enabled they allow a media field to provide an anchor point, or x and y value for use when cropping, or padding the image.
The anchor value provided can be used to specify the center point of a crop or pad.
When the setting is enabled the template must read and provide the value to the resizing helpers or filters.
The Anchors[]
is a less well known property of a MediaField
and can be accessed via the GetAnchors()
extension, or directly.
{% assign anchor = Model.ContentItem.Content.Blog.Image.Anchors.first %}
var anchors = @Model.Field.GetAnchors();
var anchors = (Anchor[])Model.ContentItem.Content.Blog.Image.Anchors.ToObject<Anchor[]>();
The Anchors[]
is kept in sync with the Paths[]
array and the index for a given path represents the index of a Anchor
value.
Query string tokens¶
When resizing images, the query string command values are, by default, signed with an HMAC signature that is unique to the tenant.
This prevents prevent malicious clients from creating too many variations of the same image.
If the UseTokenizedQueryString
is set to false
the following features will be removed.
- Cache busting, or query string versioning.
- Anchors.
- The width or height must match a value from the
SupportedSizes
configuration. - Background color.
When the query string is signed with a token any width, height value may be used.
<img src="/media/kittens.jpg?width=101&height=241&token=0J3hyv6jIPEsSdlvTCrf30fIdygkpmrF6mphqgYQyas%3D">
Note
Prior to tokens the width or height values are limited to 16
, 32
, 50
, 100
, 160
, 240
, 480
, 600
, 1024
, 2048
.
Media Content Search¶
Media can be optionally indexed for search as well if files are referenced via Media Fields. The following data can be indexed for each file referenced from a Media Field:
- Media Text
- Textual content of PDF files
Note
Standalone files, i.e. files that are just uploaded to the Media Library but never referenced from a content item via a Media Field, can't be indexed.
Note
You need an indexing implementation enabled for Media Indexing and search to work. The below guide assumes you're using Lucene.
To set up indexing for Media do the following:
- For each Media Field open the field's editor from under the given content type's or content part's editor, and tick "Include this element in the index", and tick both "Stored" and "Analyzed".
- When a content item of that type is published next time, Media content will be indexed as well. Check the name of the new Lucene field. You can do this by running a Lucene query for the given content type (can be done from the admin from under Search, Run Lucene Query): You should be able to see two new fields named with the pattern "ContentPart.FieldTechnicalName.MediaText" and "ContentPart.FieldTechnicalName.FileText", e.g. "BlogPost.File.MediaText" and "BlogPost.File.FileText".
- Configure the new field to be used for search. You can do this from the admin under Search, Settings, Search, and adding the name of the new field under "Default search fields" (arriving at something like "Content.ContentItem.FullText, BlogPost.File.MediaText, BlogPost.File.FileText").
- Try searching for content only available in the Media Text of selected media files, or referenced PDF files. You should see corresponding results.
Secure Media¶
The Secure Media feature enhances security and control over media files within the Media module.
When enabled, administrators can set view permissions for the root media folder and each first-level folder within the media root. This allows for restricting access to media folders based on user roles, ensuring that only authorized users can view or manage media files within specific folders.
New permissions to allow users to view their own media files, view media files uploaded by others, or both are created too. You can manage these among the other permissions with the Roles module.
Media files attached to content items will also adhere to the ViewContent
permission of the respective content item automatically.
Handling Unauthorized Access¶
A middleware component returns a 404 NotFound response for unauthenticated access attempts to secured media files. This not only restricts access but also conceals the existence of the file, enhancing privacy and security.
Configurable Cache-Control for Secured Files¶
The Cache-Control
header for secured files is set to no-store
by default, preventing their caching. This can be changed with the MaxSecureFilesBrowserCacheDays
configuration, see above.
Videos¶
Media Indexing¶
The Media Indexing
feature extends the media indexing capability to also encompass searching within files with the following extensions .txt
, .md
, .docx
, and .pptx
.
Credits¶
To index PDF files the PdfPig library is used. To index Microsoft Office files (i.e., .docx, .ppts) the Open-XML-SDK is used.