Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ollie Storey 13 posts 145 karma points
    22 days ago
    Ollie Storey
    0

    Get all Crops for an image?

    Is it possible to get all of the crops for an image from my generated Image model without passing in the alias(es)?

    I can see that there is a ImageCropperValue class in the Umbraco.Core namespace which has an IEnumerable<ImageCropperCrop> Crops. This would be perfect! But I cannot work out a way of turning my Image model into an ImageCropperValue?

    For context: I am building a headless implementation of Umbraco and we want to be able to add crops to the image cropper and have them automatically pull through into the API (front end) without requiring code changes to add the new alias to a list.

    v8.1.3

  • Shaishav Karnani from digitallymedia.com 289 posts 1277 karma points
    22 days ago
    Shaishav Karnani from digitallymedia.com
    100

    Hi Ollie,

    Below is the code that will help you to get dynamically Image Cropper Alias. You need to use Data Type Service and reference for GetByEditorAlias("Umbraco.ImageCropper") method. This will give you list of Image Cropper with their configuration details.

    Please review and let me know if you need any further help.

    using Umbraco.Core.PropertyEditors;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    using Umbraco.Web.Models;
    
    namespace Training.Web.Umbraco.Domain.Controllers
    {
        public class HomeController : SurfaceRenderMvcController
        {
            private readonly IDataTypeService _dataTypeService;
    
            public HomeController(IUmbracoContextFactory context, IDataTypeService dataTypeService)
            {
                _dataTypeService = dataTypeService;
            }
    
            public ActionResult Home(ContentModel model)
            {
                var dataType = _dataTypeService.GetByEditorAlias("Umbraco.ImageCropper");
                List<PreValue> toReturn = new List<PreValue>();
                if (dataType != null)
                {
                    var valueList = (ImageCropperConfiguration)dataType.First().Configuration;
    
                    if (valueList != null && valueList.Crops != null && valueList.Crops.Any())
                    {
                        toReturn.AddRange(valueList.Crops.Select(s => new PreValue
                        {
                            Width = s.Width,
                            Value = s.Alias,
                            Height = s.Height
                        }));
                    }
                }
    
                // Ignore this code
                var newModel = ModelLogic.CreateMasterModel(model.Content) as MasterModel<Home>;
                return CurrentTemplate(newModel);
            }
        }
    
        public class PreValue
        {
            public int Width { get; set; }
            public int Height { get; set; }
            public string Value { get; set; }
        }
    }
    

    Regards,

    Shaishav

Please Sign in or register to post replies

Write your reply to:

Draft