Images processor
Create a link to an asset
All file field (indexed file field and file field) share the same base structure:
- filename: mainly the original file name uploaded by the used (but it can be adjusted by the user)
- mimetype: mainly the original mimetype identified by the user's computer (but it can be adjusted by the user). If missing it will be guessed from the filename.
- sha1: the file's hash
- filesize: the file size
Example of a JSON containing a file file field:
{
"_contenttype": "asset",
"_finalization_datetime": "2020-09-29T12:43:50+0200",
"file": {
"filename": "Stakeholders_DE.png",
"filesize": 122941,
"mimetype": "image\/png",
"sha1": "aa74ddf53ca612aaf35ca12fd5ceecd8cffe10f8"
},
"_published_datetime": "2020-09-29T12:43:50+0200"
}Inside Twig, when you want to generate a link to an asset you can call the twig function ems_asset_path. Example:
<a href="{{ ems_asset_path(source.file) }}">{{ 'download.link'|trans }}</a>Processor
Regarding the file's type, it's possible to generate response from a source file. I.e. it's possible to crop an image directly from the twig. But there are parameters that works regardless the file's type:
_disposition: will specify to the browser if it should render the asset or ask the user to save it locally:- with the value
attachmentthe browser will propose to save it to the user with asave as ...dialog - with the value
inline(default value) the browser will try to display it directly in the browser if the mime-type is supported
- with the value
_file_names(array of file path) if provided the first file found will be used instead of the original asset. A file path can be a path to a local file, but most likely it's a path in a zip archive with the format{hash of the archive}:{path to file}; e.g.8ef54d1e170aede4fa78687f466f35bb6292f4ad:img/banners/banner-home.jpg_config_typespecify the processor that should process the asset. If not define the asset itself will be deliveredimagewill generate an image (PNG, SVG or JPEG) using the [image processor](#Image processor)zipwill generate a ZIP archive the [ZIP processor](#ZIP processor)
_get_file_pathif set to true will generate a server path to a file. Not an url path. To use in case of PDF generation or for local reports._mime_typeCan be used to override the file field'smimetypevalue._usernameif defined the asset will be password protected with the provided username._passwordif defined the asset will be password protected with the provided password._beforeif defined the asset will be available until before the UNIX epoch integer or the strtotime string provided._afterif defined the asset will be available after the UNIX epoch integer or the strtotime string provided._path_in_archiveIf defined, the asset will be treated as an archive, and the file at the specified path within the archive will be processed instead.
In the following example the path generated will force to download the asset:
<a href="{{ ems_asset_path(source.file, {
_disposition: 'attachment'
}) }}">{{ 'download.link'|trans }}</a>And in this sample il will generate an url to a file on the sever file system:
<a href="{{ ems_asset_path({
filename: 'Rapport.pdf',
mimetype: 'application/pdf',
sha1: 'dummy'
}, {
_file_names: [
'/opt/files/rapport.pdf',
'c:\\dev\\assets\\test_data\\rapport.pdf'
]
}) }}">{{ 'download.link'|trans }}</a>Image processor
With this processor you'll be able to generate images from a source asset:
_resizewill resize the image using one of those algorithms. Default valuefill. This parameter des not apply on SVG images:fillwill leave margins in the color defined by the_border_colorparameter (or transparent if the_qualityparameter is set to zero)fillAreawill crop to best fill the generated image without distort the image and without leave marginsfreewill distort the image to fill the imageratiofinds optimal crops for images, based on Jonas Wagner's smartcrop.jssmartCropwill compute the width or height in order to keep the original image ratio. Set_widthor_heightto0to specify the auto-size on the width or the height
_gravitywill specify the gravity to use iffillAreathe image. Default valuecenter. Possible values:centernorthsoutheastnorth-westsouth-westnorth-eastsouth-east
_qualityis an integer between 0 and 100. Default value0. Is set to 0 a PNG will be generated, otherwise is refers to the quality of the JPEG generated._backgroundcolor used to replace (semi-)transparent pixels in the format#000000. Default value#FFFFFFFF_heightdefine the height (in pixel) of the generated image is_resizeis defined. Default value200. If not define it will be computed from the_widthparameter in order to preserve the initial proportion._widthdefine the height (in pixel) of the generated image is_resizeis defined. Default value300. If not define it will be computed from the_widthparameter in order to preserve the initial proportion._radiusmake rounded corners to the image using the_border_color_radius_geometrydefine which corners must be rounded if_radiusis defined. It's an array with the list of corners to treat. Default value['topleft', 'topright', 'bottomright', 'bottomleft']Possible values:'topleft''topright''bottomright''bottomleft'
_watermark_hashhash of a PNG file to watermark the image. The PNG must present in one of the storage services defined._rotateThe source image will be rotated (in degrees). Default value0. Must contain an integer or a float._auto_rotateIf set totrue, the source image will be rotated following theOrientationdefined in the image's metadata (if defined). Default valuetrue. Possible values aretrueorfalse._flip_horizontalIf set totrue, the source image will be horizontally flipped. Default valuefalse. Possible values aretrueorfalse._flip_verticalIf set totrue, the source image will be vertically flipped. Default valuefalse. Possible values aretrueorfalse._image_formatBy default the image processor generate jpeg files, and png if the compression quality is set to 0. But you may want to use image formats by setting this parameter. Default valuenull. Possible values are:null-> will generate a jpg or a png if_qualityis equal to0webpjpegpng->_qualityis ignoredbmp->_qualityis ignoredgif->_qualityis ignored
_canonicalused to define a canonical link header in the response. For example if_canonicalis set tohttps://elasticms.fgov.be/media-files/user-groups/202304_usergroup_pv_fr.pdfthe request's headers will containLink: https://elasticms.fgov.be/media-files/user-groups/202304_usergroup_pv_fr.pdf; rel="canonical"
In this example it will generate a PNG of 400 pixels of width. The height will be defined by the proportion of the original image:
<img src="{{ ems_asset_path(source.file, {
_config_type: 'image',
_resize: 'ratio',
_width: 400,
_height: 0,
_quality: 0
}) }}">ZIP processor
{{ ems_asset_path({
filename: 'download.zip'
}, {
_config_type: 'zip',
_disposition: 'attachment',
_files: [
document.file_csv,
document.file_pdf,
]
}) }}Caution: the option _path_in_archive can not be used with the ZIP processor.
