Class ImageFileViewModelExtensions
- Namespace
- Dynamicweb.Frontend
- Assembly
- Dynamicweb.dll
Provides extension methods for the ImageFileViewModel class to create strings for background styles and GetImage URLs.
public static class ImageFileViewModelExtensions
- Inheritance
-
ImageFileViewModelExtensions
- Inherited Members
Methods
IsSvg(ImageFileViewModel?)
Determines whether the specified ImageFileViewModel instance represents an SVG image.
public static bool IsSvg(this ImageFileViewModel? imageFileViewModel)
Parameters
imageFileViewModelImageFileViewModelThe ImageFileViewModel instance to evaluate.
Returns
- bool
trueif the ImageFileViewModel instance represents an SVG image; otherwise,false.
Remarks
The method evaluates the provided imageFileViewModel instance to determine if it points to an SVG file.
Decision-Making Process:
- If
imageFileViewModelisnullor itsPathproperty is empty ornull, the method returnsfalseimmediately. - If the
Pathproperty ends with "svg" (case-insensitive), the method returnstrue, indicating the file is an SVG. - If neither condition is met, the method returns
false, as it does not match the SVG file extension requirement.
ToBackgroundStyle(ImageFileViewModel?, GetImageSettings?, bool, string)
Converts the ImageFileViewModel instance to a CSS background style string.
public static string? ToBackgroundStyle(this ImageFileViewModel? imageFileViewModel, GetImageSettings? settings = null, bool includeBackgroundPosition = true, string backgroundSize = "cover")
Parameters
imageFileViewModelImageFileViewModelThe ImageFileViewModel instance.
settingsGetImageSettingsThe settings for GetImage to control its settings like width, height and format.
includeBackgroundPositionboolA flag indicating whether to include the background position in the style.
backgroundSizestringThe background size style, default value is 'cover'. Set to
nullif background-size should not be set
Returns
- string
A CSS background style string, or null if the image file does not exist.
Remarks
This method creates its return value by:
- Checking if the imageFileViewModel or its Path is null or empty. If so, it returns null.
- Determining the background position if includeBackgroundPosition is true and the image has a focal point.
- Determining the background size style if backgroundSize is not null or empty.
- Constructing the CSS background style string with the image URL, background position, and background size.
ToBackgroundStyle(ImageFileViewModel?, bool)
Converts the ImageFileViewModel instance to a CSS background style string.
public static string? ToBackgroundStyle(this ImageFileViewModel? imageFileViewModel, bool includeBackgroundPosition = true)
Parameters
imageFileViewModelImageFileViewModelThe ImageFileViewModel instance.
includeBackgroundPositionboolA flag indicating whether to include the background position in the style.
Returns
- string
A CSS background style string, or null if the image file does not exist.
ToGetImage(ImageFileViewModel?, GetImageSettings?)
Gets the relative GetImage URL for the ImageFileViewModel instance.
public static string? ToGetImage(this ImageFileViewModel? imageFileViewModel, GetImageSettings? settings = null)
Parameters
imageFileViewModelImageFileViewModelThe ImageFileViewModel instance.
settingsGetImageSettingsThe settings for GetImage to control its settings like width, height and format.
Returns
- string
GetImage URL e.g. /admin/public/GetImage.ashx?width=1680&format=webp&image=folder/image.jpg, or null if the image file does not exist.
Remarks
Default format is webp and default width is 1680 if no settings object instance is passed.
This method creates its return value by:
- Checking if the imageFileViewModel or its Path is null or empty. If so, it returns null.
- Checking if the image is a type that is compatible with GetImage (".gif", ".jpg", ".jpeg", ".bmp", ".png", ".webp"). If not, it returns the original path without GetImage, e.g. /files/images/myIcon.svg
- Initializing the settings object if it is null and setting default width if neither width nor height is provided.
- Creating a dictionary to hold the GetImage parameters.
- Adding width, height, quality, focal point coordinates (x, y), and ratio to the parameters if they are provided.
- Calculating the height based on the ratio if provided and only width is provided.
- Adding the crop mode to the parameters if it is provided.
- Adding the format to the parameters, defaulting to webp if the format is set to Auto.
- Constructing the URL with the parameters and the encoded image path.
ToSizesVW(ImageFileViewModel?, int, int)
Generates a sizes attribute string using vw units based on the grid column spans for desktop, tablet, and mobile. Re-uses desktop width for tablet sizes and expects the number of columns rendered for tablets and desktop to be the same. Use overload to specifically set the tablet column span.
public static string? ToSizesVW(this ImageFileViewModel? imageFileViewModel, int desktopColumns, int mobileColumns)
Parameters
imageFileViewModelImageFileViewModelThe image model instance.
desktopColumnsintNumber of columns (out of 12) used on desktop.
mobileColumnsintNumber of columns (out of 12) used on mobile.
Returns
- string
A sizes attribute string for use in an img tag using vw units.
ToSizesVW(ImageFileViewModel?, int, int, int)
Generates a sizes attribute string using vw units based on the grid column spans for desktop, tablet, and mobile.
public static string? ToSizesVW(this ImageFileViewModel? imageFileViewModel, int desktopColumns, int mobileColumns, int tabletColumns)
Parameters
imageFileViewModelImageFileViewModelThe image model instance.
desktopColumnsintNumber of columns (out of 12) used on desktop.
mobileColumnsintNumber of columns (out of 12) used on mobile.
tabletColumnsintNumber of columns (out of 12) used on tablet.
Returns
- string
A sizes attribute string for use in an img tag using vw units.
ToSrcSet(ImageFileViewModel?, int, int, GetImageSettings?)
Generates a srcset string for an ImageFileViewModel that supports mobile, tablet and desktop viewports. The image width is calculated based on the provided grid column span. Re-uses desktop width for tablet sizes and expects the number of columns rendered for tablets and desktop to be the same. Use overload to specifically set the tablet column span.
public static string? ToSrcSet(this ImageFileViewModel? imageFileViewModel, int desktopColumns, int mobileColumns, GetImageSettings? baseSettings = null)
Parameters
imageFileViewModelImageFileViewModelThe image model instance.
desktopColumnsintNumber of columns (out of 12) used on desktop.
mobileColumnsintNumber of columns (out of 12) used on mobile.
baseSettingsGetImageSettingsOptional: The base GetImageSettings to use. If provided, only the width will be overwritten per breakpoint.
Returns
- string
A srcset string formatted for use in an img tag, or null if the image model is invalid.
ToSrcSet(ImageFileViewModel?, int, int, int, GetImageSettings?)
Generates a srcset string for an ImageFileViewModel that supports mobile, tablet and desktop viewports. The image width is calculated based on the provided grid column span.
public static string? ToSrcSet(this ImageFileViewModel? imageFileViewModel, int desktopColumns, int mobileColumns, int tabletColumns, GetImageSettings? settings = null)
Parameters
imageFileViewModelImageFileViewModelThe image model instance.
desktopColumnsintNumber of columns (out of 12) used on desktop.
mobileColumnsintNumber of columns (out of 12) used on mobile.
tabletColumnsintNumber of columns (out of 12) used on tablet.
settingsGetImageSettingsOptional: The base GetImageSettings to use. If provided, only the width will be overwritten per breakpoint.
Returns
- string
A srcset string formatted for use in an img tag, or null if the image model is invalid.