Advance Image Module to implement Focal Point Functionality in Sitecore

Recently, we used Advance Image Module to fulfill the requirement of setting focal point on images for different components. This module allows content authors to set focal point on image and it is cropped while rendering based on defined axes. This blog is about usage and challenges faced during implementation.

Installation:

Module is available on Sitecore Marketplace:
https://marketplace.sitecore.net/en/Modules/A/Advance_Image_Field.aspx
Download the package using above link and install it in your Sitecore instance.

Advance Image Field:

This module introduces new field type: Advance Image. This field extends existing Image field. The field editor provides all options as following:

Advance Image

When you select image for the field, it shows red pointer on selected image. It is used to set focal point of image:

Image with Focal Point

When you set focal point of image, it sets four additional properties: cropx, cropy, focusx and focusy. These properties are added to Raw Value of field:

Image Raw Values

Note: Refer challenges section for getting field editor rendered correctly.

Implementation:

Firstly, you need to clone sitecore-fields repository from GitHub. Build the solution and add reference of Sitecore.Framework.Fields.dll to your project. This is required as the dll provided in module package is not built with latest code changes in project.

You can use one of following approaches to render image:

1. Use AdvancedImageField extension method of MVC HtmlHelper to render image from View:

@Html.Sitecore().AdvancedImageField("Image Field", item, 500, 400)

2. Custom Helper method to get URL of image with focal point:

public static string GetAdvancedImageUrl(string fieldName, Item item, int height = 100, int width = 100)
{
    if (item == null)
        return string.Empty;

    var imageField = new ImageField(item.Fields[fieldName]);

    if (string.IsNullOrWhiteSpace(imageField.Value))
        return string.Empty;

	if (imageField.MediaItem == null)
        return string.Empty;

    var xml = new XmlDocument();
    xml.LoadXml(imageField.Value);

    if (xml.DocumentElement == null) return string.Empty;

    var cropx = xml.DocumentElement.HasAttribute("cropx") ? xml.DocumentElement.GetAttribute("cropx") : string.Empty;
    var cropy = xml.DocumentElement.HasAttribute("cropy") ? xml.DocumentElement.GetAttribute("cropy") : string.Empty;
    var focusx = xml.DocumentElement.HasAttribute("focusx") ? xml.DocumentElement.GetAttribute("focusx") : string.Empty;
    var focusy = xml.DocumentElement.HasAttribute("focusy") ? xml.DocumentElement.GetAttribute("focusy") : string.Empty;

    float.TryParse(cropx, out float cx);
    float.TryParse(cropy, out float cy);
    float.TryParse(focusx, out float fx);
    float.TryParse(focusy, out float fy);    

    var imageSrc = MediaManager.GetMediaUrl(imageField.MediaItem);

    var src = $"{imageSrc}?cx={cx}&cy={cy}&cw={width}&ch={height}";

    var hash = HashingUtils.GetAssetUrlHash(src);

    return $"{src}&hash={hash}";
}

Challenges:

1. When you use Advance Image field type, it should render all options of existing Image field type as following:

Advance Image

But it renders as simple text-box. Follow below steps to render field editor correctly:

  1. Switch to Core database
  2. Select /sitecore/system/Field Types/Advanced Field Types item.
  3. In the Configure tab, press the Change button in the Template section, and select /sitecore/Common/Folder

2. This field does not support image editing in Experience Editor. So, we used Sitecore Edit Frame to enable editing in Experience Editor. Refer Using Edit Frames in Sitecore document.

3. AdvancedImageField extension method of HtmlHelper throws exception in case of following scenarios:

  • The field value is empty
  • Selected media item does not exist in Sitecore.

We added condition to verify these scenarios.

This code change is committed here: https://github.com/ksorthiya/sitecore-fields
File which contains the change: https://github.com/ksorthiya/sitecore-fields/blob/master/src/Sitecore.Fields/Extensions/HtmlExtensions.cs

4. This module overrides ThumbnailProcessor of getMediaStream pipeline with CropProcessor. This processor does not have condition to run default code of ThumbnailProcessor when media item is not of image type. So, whenever we navigated to PDF item in Sitecore content editor, the PDF attached to item was downloaded and also, thumbnail of item was missing. We fixed this issue by adding condition to run default logic for getting thumbnail.

This code change is committed here: https://github.com/ksorthiya/sitecore-fields
File which contains the change: https://github.com/ksorthiya/sitecore-fields/blob/master/src/Sitecore.Fields/Processors/CropProcessor.cs

You can find more information regarding this module on following resources:

https://marketplace.sitecore.net/en/Modules/A/Advance_Image_Field.aspx
https://github.com/saadahmedkhan/sitecore-fields
https://inverseproportion.wordpress.com/2017/04/12/image-field-with-focus-cropper-in-sitecore/

Advertisements

Tagged: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: