Created
May 24, 2022 08:33
-
-
Save skttl/f9a4c76463c54b859d5ebff155a6f614 to your computer and use it in GitHub Desktop.
Get object-position from FocalPoint in Umbraco
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Globalization; | |
| using Umbraco.Cms.Core.Media; | |
| using Umbraco.Cms.Core.Models; | |
| using Umbraco.Cms.Core.Models.PublishedContent; | |
| using Umbraco.Cms.Core.PropertyEditors.ValueConverters; | |
| using Umbraco.Extensions; | |
| using UmbConstants = Umbraco.Cms.Core.Constants; | |
| namespace Extensions | |
| { | |
| public static class MediaPublishedContentExtensions | |
| { | |
| /// <summary> | |
| /// Gets the value for css property object-position, based on the focal point set in Umbraco | |
| /// </summary> | |
| /// <param name="node"></param> | |
| /// <param name="precision">How many decimals to show, defaults to 2</param> | |
| /// <param name="propertyAlias">Property alias containing the imagecropper</param> | |
| /// <returns></returns> | |
| public static string GetObjectPosition(this IPublishedContent source, string propertyAlias = UmbConstants.Conventions.Media.File) | |
| { | |
| return source.GetObjectPositionX() + " " + source.GetObjectPositionY(); | |
| } | |
| /// <summary> | |
| /// Gets the value for the x-part of css property object-position, based on the focal point set in Umbraco | |
| /// </summary> | |
| /// <param name="node"></param> | |
| /// <param name="precision">How many decimals to show, defaults to 2</param> | |
| /// <param name="propertyAlias">Property alias containing the imagecropper</param> | |
| public static string GetObjectPositionX(this IPublishedContent source, string propertyAlias = UmbConstants.Conventions.Media.File) | |
| { | |
| return Math.Round(source.GetFocalPointX(propertyAlias), 2).ToString(CultureInfo.InvariantCulture) + "%"; | |
| } | |
| /// <summary> | |
| /// Gets the value for the y-part of css property object-position, based on the focal point set in Umbraco | |
| /// </summary> | |
| /// <param name="node"></param> | |
| /// <param name="precision">How many decimals to show, defaults to 2</param> | |
| /// <param name="propertyAlias">Property alias containing the imagecropper</param> | |
| public static string GetObjectPositionY(this IPublishedContent source, string propertyAlias = UmbConstants.Conventions.Media.File) | |
| { | |
| return Math.Round(source.GetFocalPointY(propertyAlias), 2).ToString(CultureInfo.InvariantCulture) + "%"; | |
| } | |
| public static decimal GetFocalPointX(this IPublishedContent source, string propertyAlias = UmbConstants.Conventions.Media.File) | |
| { | |
| try | |
| { | |
| var dataSet = source.Value<ImageCropperValue>(propertyAlias); | |
| return dataSet.HasFocalPoint() ? dataSet.FocalPoint.Left * 100 : 50; | |
| } | |
| catch (Exception) | |
| { | |
| return 50; | |
| } | |
| } | |
| public static decimal GetFocalPointY(this IPublishedContent source, string propertyAlias = UmbConstants.Conventions.Media.File) | |
| { | |
| try | |
| { | |
| var dataSet = source.Value<ImageCropperValue>(propertyAlias); | |
| return dataSet.HasFocalPoint() ? dataSet.FocalPoint.Top * 100 : 50; | |
| } | |
| catch (Exception) | |
| { | |
| return 50; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment