Exemplo n.º 1
0
        public Image GetImage(IPublishedContent node, int?width = null, int?height = null,
                              ImageCropMode imageCropMode       = ImageCropMode.Crop, string nameSpace = "picture")

        {
            if (node == null)
            {
                return(null);
            }

            var media = Helper.Media(node.Id);

            if (media == null)
            {
                throw new ImageException("Node is null or no Media with this Node found");
            }

            return(new Image()
            {
                Sources = GetImageSources(media, width, height, imageCropMode),
                Alt = media.Name,
                Height = height.HasValue ? $"{height}px" : null,
                Width = width.HasValue ? $"{width}px" : null,
                Namespace = nameSpace,
                ImageNode = node
            });
        }
Exemplo n.º 2
0
    /// <summary>
    ///     Gets a processed image for the image at the given path
    /// </summary>
    /// <param name="imagePath"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="focalPointLeft"></param>
    /// <param name="focalPointTop"></param>
    /// <param name="mode"></param>
    /// <param name="cacheBusterValue"></param>
    /// <param name="cropX1"></param>
    /// <param name="cropX2"></param>
    /// <param name="cropY1"></param>
    /// <param name="cropY2"></param>
    /// <returns></returns>
    /// <remarks>
    ///     If there is no media, image property or image file is found then this will return not found.
    /// </remarks>
    public string?GetProcessedImageUrl(
        string imagePath,
        int?width               = null,
        int?height              = null,
        decimal?focalPointLeft  = null,
        decimal?focalPointTop   = null,
        ImageCropMode mode      = ImageCropMode.Max,
        string cacheBusterValue = "",
        decimal?cropX1          = null,
        decimal?cropX2          = null,
        decimal?cropY1          = null,
        decimal?cropY2          = null)
    {
        var options = new ImageUrlGenerationOptions(imagePath)
        {
            Width            = width,
            Height           = height,
            ImageCropMode    = mode,
            CacheBusterValue = cacheBusterValue
        };

        if (focalPointLeft.HasValue && focalPointTop.HasValue)
        {
            options.FocalPoint =
                new ImageUrlGenerationOptions.FocalPointPosition(focalPointLeft.Value, focalPointTop.Value);
        }
        else if (cropX1.HasValue && cropX2.HasValue && cropY1.HasValue && cropY2.HasValue)
        {
            options.Crop =
                new ImageUrlGenerationOptions.CropCoordinates(cropX1.Value, cropY1.Value, cropX2.Value, cropY2.Value);
        }

        return(_imageUrlGenerator.GetImageUrl(options));
    }
        public TypedPictureElement(IPublishedContent content, string propertyAlias = "umbracoFile", ImageCropMode imageCropMode = ImageCropMode.Crop)
            : base()
        {
            if (content == null)
                throw new System.ArgumentNullException("Content", "Missing Content from Picture. Use Umbraco.Picture(IPublishedContent)");

            ImageCropMode = imageCropMode;
            Content = content;
            PropertyAlias = propertyAlias;
        }
Exemplo n.º 4
0
        public TypedPictureElement(IPublishedContent content, string propertyAlias = "umbracoFile", ImageCropMode imageCropMode = ImageCropMode.Crop)
            : base()
        {
            if (content == null)
            {
                throw new System.ArgumentNullException("Content", "Missing Content from Picture. Use Umbraco.Picture(IPublishedContent)");
            }

            ImageCropMode = imageCropMode;
            Content       = content;
            PropertyAlias = propertyAlias;
        }
Exemplo n.º 5
0
 public IEnumerable <Image> GetImages(IEnumerable <IPublishedContent> contentNodes, int?width = null, int?height = null,
                                      ImageCropMode imageCropMode = ImageCropMode.Crop, string nameSpace         = "picture")
 {
     return(contentNodes?.Select(n => new Image()
     {
         Sources = GetImageSources(n, width, height, imageCropMode),
         Alt = n.Name,
         Height = height.HasValue ? $"{height}px" : null,
         Width = width.HasValue ? $"{width}px" : null,
         Namespace = nameSpace,
         ImageNode = n
     }) ?? new List <Image>());
 }
Exemplo n.º 6
0
 public static string GetCropUrl(
     this IImage image, 
     int? width = null, 
     int? height = null,
     string propertyAlias = Constants.Conventions.Media.File, 
     string cropAlias = null, 
     int? quality = null,
     ImageCropMode? imageCropMode = null, 
     ImageCropAnchor? imageCropAnchor = null, 
     bool preferFocalPoint = false,
     bool useCropDimensions = false, 
     bool cacheBuster = true, 
     string furtherOptions = null,
     ImageCropRatioMode? ratioMode = null, 
     bool upScale = true)
 {
     return image.Content.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
 }
 /// <summary>
 /// Return a crop URL of the image based on the specified parameters.
 /// </summary>
 /// <param name="width">The width of the output image.</param>
 /// <param name="height">The height of the output image.</param>
 /// <param name="propertyAlias">Property alias of the property containing the Json data.</param>
 /// <param name="cropAlias">The crop alias.</param>
 /// <param name="quality">Quality percentage of the output image.</param>
 /// <param name="imageCropMode">The image crop mode.</param>
 /// <param name="imageCropAnchor">The image crop anchor.</param>
 /// <param name="preferFocalPoint">Use focal point, to generate an output image using the focal point instead of the predefined crop.</param>
 /// <param name="useCropDimensions">Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters.</param>
 /// <param name="cacheBuster">Add a serialised date of the last edit of the item to ensure client cache refresh when updated.</param>
 /// <param name="furtherOptions">The further options.</param>
 /// <param name="ratioMode">Use a dimension as a ratio.</param>
 /// <param name="upScale">If the image should be upscaled to requested dimensions.</param>
 /// <returns>Returns a string with the crop URL.</returns>
 public string GetCropUrl(int? width = null, int? height = null, string propertyAlias = "umbracoFile", string cropAlias = null, int? quality = null, ImageCropMode? imageCropMode = null, ImageCropAnchor? imageCropAnchor = null, bool preferFocalPoint = false, bool useCropDimensions = false, bool cacheBuster = true, string furtherOptions = null, ImageCropRatioMode? ratioMode = null, bool upScale = true) {
     return Image.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
 }
Exemplo n.º 8
0
        public static IEnumerable <Image> GetImages(this IPublishedElement element, string field, int?width = null, int?height = null, ImageCropMode imageCropMode = ImageCropMode.Crop)
        {
            var imageService = (IImageService)DependencyResolver.Current.GetService(typeof(IImageService));

            return(element.HasValue(field)
                ? imageService.GetImages(element.Value <IEnumerable <IPublishedContent> >(field), width, height, imageCropMode)
                : null);
        }
        /// <summary>
        /// (Works for Images and svg) Gets the img tag with the cropped media item using its alternative text and website's image quality setting.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="cropAlias"></param>
        /// <param name="cssClass"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="cropMode"></param>
        /// <returns></returns>
        public static HtmlString GetCroppedImage(this IPublishedContent image, string cropAlias, string cssClass = null, object htmlAttributes = null, ImageCropMode imageCropMode = ImageCropMode.Crop)
        {
            if (image == null)
            {
                return(null);
            }

            var tagBuilder = new TagBuilder("img");

            tagBuilder.MergeAttribute("src", image.GetCroppedImageUrl(cropAlias, imageCropMode));
            tagBuilder.MergeAttribute("class", cssClass);
            tagBuilder.MergeAttribute("alt", image.Value <string>("AltText"));

            RenderAttributes(htmlAttributes, tagBuilder);

            return(new HtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing) + Environment.NewLine));
        }
        /// <summary>
        /// Gets the crop URL for the media item using the website's image quality setting.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="cropAlias"></param>
        /// <param name="cropMode"></param>
        /// <returns></returns>
        public static string GetCroppedImageUrl(this IPublishedContent image, string cropAlias, ImageCropMode imageCropMode = ImageCropMode.Crop)
        {
            var url = image?.GetCropUrl(cropAlias: cropAlias, useCropDimensions: true, quality: ImageQuality, imageCropMode: imageCropMode);

            return(url);
        }
Exemplo n.º 11
0
        private IEnumerable <ImageSource> GetImageSources(IPublishedContent media, int?width = null, int?height = null, ImageCropMode imageCropMode = ImageCropMode.Crop)
        {
            var result     = new List <ImageSource>();
            var dimensions = Enum.GetValues(typeof(EImageDimension)).Cast <EImageDimension>();

            foreach (var dimension in dimensions)
            {
                int?calculatedWidth;
                int?calculatedHeight;

                switch (dimension)
                {
                case EImageDimension.Small:
                    calculatedHeight = height.HasValue ? (int?)(height.Value * 0.2) : null;
                    calculatedWidth  = width.HasValue ? (int?)(width.Value * 0.2) : height.HasValue ? null : (int?)600;
                    break;

                case EImageDimension.Medium:
                    calculatedHeight = height.HasValue ? (int?)(height.Value * 0.5) : null;
                    calculatedWidth  = width.HasValue ? (int?)(width.Value * 0.5) : height.HasValue ? null : (int?)985;
                    break;

                case EImageDimension.Large:
                    calculatedHeight = height.HasValue ? (int?)(height.Value * 1.2) : null;
                    calculatedWidth  = width.HasValue ? (int?)(width.Value * 1.2) : height.HasValue ? null : (int?)1150;
                    break;

                case EImageDimension.ExtraLarge:
                    calculatedHeight = height.HasValue ? (int?)(height.Value * 1.5) : null;
                    calculatedWidth  = width.HasValue ? (int?)(width.Value * 1.5) : height.HasValue ? null : (int?)1600;
                    break;

                default:
                    calculatedHeight = height;
                    calculatedWidth  = width;
                    break;
                }



                //var mimeType = media.HasProperty("umbracoExtension") && media.HasValue("umbracoExtension")
                //    ? $"image/{media.Value<string>("umbracoExtension")}".Replace("jpg", "jpeg")
                //    : string.Empty;

                result.Add(new ImageSource()
                {
                    Size = dimension,
                    Src  = media.GetCropUrl(width: calculatedWidth, height: calculatedHeight, imageCropMode: imageCropMode, furtherOptions: "&format=webp&quality=80"),
                    Type = "image/webp"
                });
            }

            return(result);
        }
Exemplo n.º 12
0
        public IEnumerable <ArticlePage> GetTeaser(IEnumerable <IPublishedContent> content, int?width = null, int?height = null, ImageCropMode cropMode = ImageCropMode.Crop)
        {
            var result = new List <ArticlePage>();

            if (content != null)
            {
                foreach (var c in content)
                {
                    result.Add(new ArticlePage(c)
                    {
                        EmotionImages = c.HasValue(DocumentTypes.ArticlePage.Fields.EmotionImages) ?
                                        _imageService.GetImages(c.Value <IEnumerable <IPublishedContent> >(DocumentTypes.ArticlePage.Fields.EmotionImages), width,
                                                                height, cropMode) :
                                        new List <Image>()
                    });
                }
            }

            return(result);
        }