-
Notifications
You must be signed in to change notification settings - Fork 15
scale2w
Scale to Width. The "scale2w" Snippet is an output filter that operates on an asset ID and returns a URL to the image of the desired size. It scales the asset to a desired width and calculates the height to preserve the aspect ratio of the original image. This request will trigger the desired image size to be generated if it does not exist already.
Specify the desired width as an integer (in pixels).
Normally, the asset_id parameter would be supplied via the getPageAssets Snippet.
WARNING: use of an output filter inside a "formatting string" does not work!
This won't work, unfortunately:
[[getPageAssets?
&innerTpl=`<img src="[[+asset_id:scale2w=`600`]]"
width="600"
height="[[+asset_id.height]]"
alt="Whoops, this does not work!"/>`
]]
Instead, if you need to use an output filter, create a traditional MODX chunk, e.g. myInnerTpl:
<img src="[[+asset_id:scale2w=`600`]]"
width="600"
height="[[+asset_id.height]]" />
Then reference the Chunk by name:
[[getPageAssets?
&innerTpl=`myInnerTpl`
]]
The calculated width is available as a placeholder: [[+asset_id.width]]
Although unconventional, it is possible to call the scale2w Snippet as a stand-alone Snippet and not as an output filter. This is useful if you wanted to display a single asset at a desired size. In the example below, asset ID 123 will be resized to a width of 600.
<img src="[[scale2w? &input=`123` &options=`600`]]"
width="600" height="[[+asset_id.height]]" />
Note that this usage would be equivalent to using the Asset Snippet:
[[Asset?
&asset_id=`123`
&width=`600`
&tpl=`<img src="[[+url]]" width="600" height="[[+asset_id.height]]" />`
]]
© 2014 and beyond by Craftsman Coding