Class ImageFileViewModelExtensions
- Namespace
- Dynamicweb.Frontend
- Assembly
- Dynamicweb.dll
Provides extension methods for the Image
- Inheritance
-
Image
File View Model Extensions
- Inherited Members
Methods
IsSvg(ImageFileViewModel?)
Determines whether the specified Image
Parameters
imageFileViewModel
ImageFile View Model The Image
File instance to evaluate.View Model
Returns
- bool
true
if the ImageFile instance represents an SVG image; otherwise,View Model false
.
Remarks
The method evaluates the provided imageFileViewModel
instance to determine if it points to an SVG file.
Decision-Making Process:
- If
imageFileViewModel
isnull
or itsPath
property is empty ornull
, the method returnsfalse
immediately. - If the
Path
property 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 Image
public static string? ToBackgroundStyle(this ImageFileViewModel? imageFileViewModel, GetImageSettings? settings = null, bool includeBackgroundPosition = true, string backgroundSize = "cover")
Parameters
imageFileViewModel
ImageFile View Model The ImageFileViewModel instance.
settings
GetImage Settings The settings for GetImage to control its settings like width, height and format.
includeBackgroundPosition
boolA flag indicating whether to include the background position in the style.
backgroundSize
stringThe background size style, default value is 'cover'. Set to
null
if 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
imageFileViewModel
ImageFile View Model The ImageFileViewModel instance.
includeBackgroundPosition
boolA 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 Image
public static string? ToGetImage(this ImageFileViewModel? imageFileViewModel, GetImageSettings? settings = null)
Parameters
imageFileViewModel
ImageFile View Model The ImageFileViewModel instance.
settings
GetImage Settings The 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.