Link.Image(...) / @Link.Image Method in Dynamic Code
Linking images - especially images which should be automatically resized - can be tricky. This is where Link.Image(...)
helps.
Tip
2sxc 13.10 adds a new ImageService which helps generate the best possible HTML for images.
Only use the Link.Image(...)
where really necessary, because the IImageService
is usually the better choice.
Simple Examples
<!-- simple example -->
<img src='@Link.Image(Content.MainPicture)'>
<!-- simple example with resize -->
<img src='@Link.Image(url: "test.jpg", width: 250, height: 700)'>
<!-- simple example with resize and custom quality -->
<img src='@Link.Image(url: "test.jpg", width: 250, height: 700, quality: 75.2)'>
Using Preconfigured Image Sizes
The Image
method can also use predefined image configuration. This uses the new Settings System.
<!-- Resize an image to the default of content -->
<img src='@Link.Image(url: "test.jpg", settings: Settings.Images.Content)'>
<!-- Resize an image to the default of lightboxes -->
<img src='@Link.Image(url: "test.jpg", settings: Settings.Images.Lightbox)'>
<!-- Resize an image to the default of screen / backgrounds -->
<img src='@Link.Image(url: "test.jpg", settings: Settings.Images.Screen)'>
<!-- Resize an image to HALF the size of a default content -->
<img src='@Link.Image(url: "test.jpg", settings: Settings.Images.Content, factor: 0.5)'>
Tip
This is very powerful and is the recommended way to resize images, because this way all images follow the same sizes / proportions in all your apps.
Parameters
To promote long term API stability, we require most parameters to be named when used. This allows us to add further parameters later on, and the calls will still work.
The first three parameters don't need naming, as they will be the most used. They are these in this order:
url
- the only required parametersettings
- optional, would be an image-settings configurationfactor
- optional multiplier - eg. 0.5 to be half the size of the preset configuration
All other parameters need to be named:
width
width in pixelsheight
height in pixelsquality
a quality factor between 1 and 100; eg. jpgs usually have 50 - 80resizeMode
which can bemax
,crop
,pad
etc.scaleMode
which can beup
,down
(default) orboth
format
which can bejpg
,png
,gif
aspectRatio
So this will work:
@Link.Image("test.jpg", Settings.Images.Content, 0.5)
@Link.Image(url: "test.jpg", settings: Settings.Images.Content, factor: 0.5)
@Link.Image("test.jpg", width: 200)
...and this won't:
@Link.Image("test.jpg", 200)
Demo App and further links
🎓 Image Resizer Parameters with Link.Image
📔 Images Guide (Best Practices & Code)
History
- Introduced in 2sxc 12.04
- For many cases you should now use the ImageService in 2sxc 13.10+