Fetching image cropper cropped file path

IPublished content extension for getting crop image file easily

  • Share

ImageCropper datatype was once part of uComponents, but because it is so usefull, it is now included into standard Umbraco instalation.

However as much it is useful, I could not find the way to get cropped image file properly with anything that was shipped with Umbraco installation. Therefore I decided to write my own extension and to make life easier while working with ImageCropper in Umbraco.

        public static string GetCropImageUrl(this IPublishedContent media, string cropName)
        {
            string result =  string.Empty;
            if (media != null)
            {
                string filename;
                result = media.Url.Substring(0, media.Url.LastIndexOf("/"));
                filename = media.Url.Substring(media.Url.LastIndexOf("/"));
                filename = string.Concat(
                    filename.Substring(0, filename.LastIndexOf(".")),
                    "_", cropName,
                    filename.Substring(filename.LastIndexOf("."))
                    );
                result = string.Concat(result, filename);
            }
            return result;
        }
    

This is nothing by a string manipulation function which adds _cropname to a media file path.

Note

This extension method does not check whether file is created or not by cropper. It will always return a string value based on crop name parameter and media file URL path

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus