Skip to content
fireproofsocks edited this page Sep 25, 2014 · 7 revisions

scale2h

Scale to Height. The "scale2h" Snippet is an output filter that operates on an asset ID and returns a URL to the image of the desired dimensions. It scales the asset to a desired height and calculates the width 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.

Arguments

Specify the desired height as an integer (in pixels).

Example

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!

[[getPageAssets? 
    &innerTpl=`<img src="[[+asset_id:scale2h=`500`]]" 
     width="[[+asset_id.width]]" 
     height="500" 
     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:scale2h=`500`]]" 
     width="[[+asset_id.width]]" 
     height="500" />

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 scale2h 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 height of 400.

<img src="[[scale2h? &input=`123` &options=`400`]]" width="[[+asset_id.width]]" height="400" />

Note that this usage would be equivalent to using the Asset Snippet:

[[Asset? 
    &asset_id=`123`
    &height=`400` 
    &tpl=`<img src="[[+url]]" width="[[+asset_id.width]]" height="400" />`
]]
Clone this wiki locally