Table of Contents

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

imageFileViewModel ImageFileViewModel

The ImageFileViewModel instance to evaluate.

Returns

bool

true if 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 imageFileViewModel is null or its Path property is empty or null, the method returns false immediately.
  • If the Path property ends with "svg" (case-insensitive), the method returns true, 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

imageFileViewModel ImageFileViewModel

The ImageFileViewModel instance.

settings GetImageSettings

The settings for GetImage to control its settings like width, height and format.

includeBackgroundPosition bool

A flag indicating whether to include the background position in the style.

backgroundSize string

The 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 ImageFileViewModel

The ImageFileViewModel instance.

includeBackgroundPosition bool

A 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

imageFileViewModel ImageFileViewModel

The ImageFileViewModel instance.

settings GetImageSettings

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.
To top