Example #1
0
        public static String GetResponsiveImageURL(String imageURL, Int32 width = 0, Int32 height = 0)
        {
            if (String.IsNullOrWhiteSpace(imageURL) || imageURL.Contains("getmedia"))
            {
                return(imageURL);
            }

            try
            {
                var imagePath = imageURL.Replace("~", String.Empty).Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

                MediaLibraryInfo mediaLibrary;

                if (!imagePath[2].Equals("assets"))
                {
                    mediaLibrary = CacheHelpers.GetCachedMediaLibriaryInfo(imagePath[2]);
                }
                else
                {
                    mediaLibrary = null;
                    EventLogProvider.LogInformation("GetResponsiveImageURL", EventType.INFORMATION, "Could not get media library for file: " + imageURL);
                }

                if (mediaLibrary != null)
                {
                    // get the file now by the media library and the path
                    var imageName = imagePath.Last().Split(new[] { "?" }, StringSplitOptions.RemoveEmptyEntries)[0];
                    var file      = CacheHelpers.GetCachedMediaFileInfo(mediaLibrary.LibraryID, GetImagePath(imagePath, imageName));

                    if (file != null && width == 0)
                    {
                        return(String.Format("/getmedia/{0}/{1}", file.FileGUID, file.FileName));
                    }

                    if (file != null && width > 0 && height == 0)
                    {
                        return(String.Format("/getmedia/{0}/{1}?width={2}&ext={3}", file.FileGUID, file.FileName, width, file.FileExtension));
                    }

                    if (file != null && width > 0 && height > 0)
                    {
                        return(String.Format("/getmedia/{0}/{1}?width={2}&height={3}&ext={4}", file.FileGUID, file.FileName, width, height, file.FileExtension));
                    }
                }
            }
            catch (Exception ex)
            {
                EventLogProvider.LogException("GetResponsiveImageURL", EventType.ERROR, ex, SiteContext.CurrentSiteID, "Unabled to resize image");
            }

            return(imageURL);
        }