Skip to content

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

The width and height arguments are limited to a specific list of values to prevent malicious clients from creating too many variations of the same image.
The values can be 16, 32, 50, 100, 160, 240, 480, 600, 1024, 2048.

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 ouput 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" />

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 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)

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" />

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 injecting OrchardCore.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
      "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 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 path used to store media assets. The path can be relative to the tenant's App_Data folder, or absolute.
      "AssetsPath": "Media",

      // 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'"
    }

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 => ...);

CREDITS

ImageSharp

https://sixlabors.com/products/imagesharpweb/
Copyright 2012 James South
Licensed under the Apache License, Version 2.0, or with a commercial support license https://sixlabors.com/pricing


Last update: September 23, 2020