예제 #1
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));
    }
 private void AppendFocalPoint(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options)
 {
     imageProcessorUrl.Append("?center=");
     imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture)).Append(",");
     imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
     imageProcessorUrl.Append("&mode=crop");
 }
 private void AppendCrop(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options)
 {
     imageProcessorUrl.Append("?crop=");
     imageProcessorUrl.Append(options.Crop.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
     imageProcessorUrl.Append(options.Crop.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
     imageProcessorUrl.Append(options.Crop.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
     imageProcessorUrl.Append(options.Crop.Y2.ToString(CultureInfo.InvariantCulture));
     imageProcessorUrl.Append("&cropmode=percentage");
 }
예제 #4
0
        public string GetImageUrl(ImageUrlGenerationOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var imageUrl = new StringBuilder(options.ImageUrl);

            var queryStringHasStarted = false;

            void AppendQueryString(string value)
            {
                imageUrl.Append(queryStringHasStarted ? '&' : '?');
                queryStringHasStarted = true;

                imageUrl.Append(value);
            }

            void AddQueryString(string key, params IConvertible[] values)
예제 #5
0
            public string GetImageUrl(ImageUrlGenerationOptions options)
            {
                var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);

                if (options.FocalPoint != null)
                {
                    imageProcessorUrl.Append("?f=");
                    imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture));
                    imageProcessorUrl.Append("x");
                    imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
                }
                else if (options.Crop != null)
                {
                    imageProcessorUrl.Append("?c=");
                    imageProcessorUrl.Append(options.Crop.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
                    imageProcessorUrl.Append(options.Crop.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
                    imageProcessorUrl.Append(options.Crop.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
                    imageProcessorUrl.Append(options.Crop.Y2.ToString(CultureInfo.InvariantCulture));
                }
                else if (options.DefaultCrop)
                {
                    imageProcessorUrl.Append("?m=defaultcrop");
                }
                else
                {
                    imageProcessorUrl.Append("?m=" + options.ImageCropMode.ToString().ToLower());
                    if (options.ImageCropAnchor != null)
                    {
                        imageProcessorUrl.Append("&a=" + options.ImageCropAnchor.ToString().ToLower());
                    }
                }

                var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&f=");

                if (options.Quality != null && hasFormat == false)
                {
                    imageProcessorUrl.Append("&q=" + options.Quality);
                }
                if (options.HeightRatio != null)
                {
                    imageProcessorUrl.Append("&hr=" + options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
                }
                if (options.WidthRatio != null)
                {
                    imageProcessorUrl.Append("&wr=" + options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
                }
                if (options.Width != null)
                {
                    imageProcessorUrl.Append("&w=" + options.Width);
                }
                if (options.Height != null)
                {
                    imageProcessorUrl.Append("&h=" + options.Height);
                }
                if (options.UpScale == false)
                {
                    imageProcessorUrl.Append("&u=no");
                }
                if (options.AnimationProcessMode != null)
                {
                    imageProcessorUrl.Append("&apm=" + options.AnimationProcessMode);
                }
                if (options.FurtherOptions != null)
                {
                    imageProcessorUrl.Append(options.FurtherOptions);
                }
                if (options.Quality != null && hasFormat)
                {
                    imageProcessorUrl.Append("&q=" + options.Quality);
                }
                if (options.CacheBusterValue != null)
                {
                    imageProcessorUrl.Append("&r=").Append(options.CacheBusterValue);
                }

                return(imageProcessorUrl.ToString());
            }
예제 #6
0
        /// <summary>
        /// Gets the ImageProcessor Url from the image path.
        /// </summary>
        /// <param name="imageUrl">
        /// The image url.
        /// </param>
        /// <param name="imageUrlGenerator">
        /// The generator that will process all the options and the image URL to return a full image urls with all processing options appended
        /// </param>
        /// <param name="cropDataSet"></param>
        /// <param name="width">
        /// The width of the output image.
        /// </param>
        /// <param name="height">
        /// The height of the output image.
        /// </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 if there is one
        /// </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="cacheBusterValue">
        /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated
        /// </param>
        /// <param name="furtherOptions">
        /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example:
        /// <example>
        /// <![CDATA[
        /// furtherOptions: "&bgcolor=fff"
        /// ]]>
        /// </example>
        /// </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>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetCropUrl(
            this string imageUrl,
            IImageUrlGenerator imageUrlGenerator,
            ImageCropperValue cropDataSet,
            int?width                       = null,
            int?height                      = null,
            string cropAlias                = null,
            int?quality                     = null,
            ImageCropMode?imageCropMode     = null,
            ImageCropAnchor?imageCropAnchor = null,
            bool preferFocalPoint           = false,
            bool useCropDimensions          = false,
            string cacheBusterValue         = null,
            string furtherOptions           = null,
            ImageCropRatioMode?ratioMode    = null,
            bool upScale                    = true,
            string animationProcessMode     = null)
        {
            if (string.IsNullOrEmpty(imageUrl))
            {
                return(string.Empty);
            }

            ImageUrlGenerationOptions options;

            if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null))
            {
                var crop = cropDataSet.GetCrop(cropAlias);

                // if a crop was specified, but not found, return null
                if (crop == null && !string.IsNullOrWhiteSpace(cropAlias))
                {
                    return(null);
                }

                options = cropDataSet.GetCropBaseOptions(imageUrl, crop, string.IsNullOrWhiteSpace(cropAlias), preferFocalPoint);

                if (crop != null & useCropDimensions)
                {
                    width  = crop.Width;
                    height = crop.Height;
                }

                // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a width parameter has been passed we can get the crop ratio for the height
                if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width != null && height == null)
                {
                    options.HeightRatio = (decimal)crop.Height / crop.Width;
                }

                // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a height parameter has been passed we can get the crop ratio for the width
                if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width == null && height != null)
                {
                    options.WidthRatio = (decimal)crop.Width / crop.Height;
                }
            }
            else
            {
                options = new ImageUrlGenerationOptions(imageUrl)
                {
                    ImageCropMode   = (imageCropMode ?? ImageCropMode.Pad).ToString().ToLowerInvariant(),
                    ImageCropAnchor = imageCropAnchor?.ToString().ToLowerInvariant()
                };
            }

            options.Quality = quality;
            options.Width   = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width;
            options.Height  = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height;
            options.AnimationProcessMode = animationProcessMode;

            if (ratioMode == ImageCropRatioMode.Width && height != null)
            {
                // if only height specified then assume a square
                if (width == null)
                {
                    width = height;
                }
                options.WidthRatio = (decimal)width / (decimal)height;
            }

            if (ratioMode == ImageCropRatioMode.Height && width != null)
            {
                // if only width specified then assume a square
                if (height == null)
                {
                    height = width;
                }
                options.HeightRatio = (decimal)height / (decimal)width;
            }

            options.UpScale          = upScale;
            options.FurtherOptions   = furtherOptions;
            options.CacheBusterValue = cacheBusterValue;

            return(imageUrlGenerator.GetImageUrl(options));
        }
        public string GetImageUrl(ImageUrlGenerationOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);

            if (options.FocalPoint != null)
            {
                AppendFocalPoint(imageProcessorUrl, options);
            }
            else if (options.Crop != null)
            {
                AppendCrop(imageProcessorUrl, options);
            }
            else if (options.DefaultCrop)
            {
                imageProcessorUrl.Append("?anchor=center&mode=crop");
            }
            else
            {
                imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? "crop").ToLower());

                if (options.ImageCropAnchor != null)
                {
                    imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToLower());
                }
            }

            var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");

            //Only put quality here, if we don't have a format specified.
            //Otherwise we need to put quality at the end to avoid it being overridden by the format.
            if (options.Quality != null && hasFormat == false)
            {
                imageProcessorUrl.Append("&quality=").Append(options.Quality);
            }
            if (options.HeightRatio != null)
            {
                imageProcessorUrl.Append("&heightratio=").Append(options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
            }
            if (options.WidthRatio != null)
            {
                imageProcessorUrl.Append("&widthratio=").Append(options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
            }
            if (options.Width != null)
            {
                imageProcessorUrl.Append("&width=").Append(options.Width);
            }
            if (options.Height != null)
            {
                imageProcessorUrl.Append("&height=").Append(options.Height);
            }
            if (options.UpScale == false)
            {
                imageProcessorUrl.Append("&upscale=false");
            }
            if (options.AnimationProcessMode != null)
            {
                imageProcessorUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode);
            }
            if (options.FurtherOptions != null)
            {
                imageProcessorUrl.Append(options.FurtherOptions);
            }

            //If furtherOptions contains a format, we need to put the quality after the format.
            if (options.Quality != null && hasFormat)
            {
                imageProcessorUrl.Append("&quality=").Append(options.Quality);
            }
            if (options.CacheBusterValue != null)
            {
                imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
            }

            return(imageProcessorUrl.ToString());
        }
예제 #8
0
    /// <summary>
    ///     Gets the underlying image processing service URL from the image path.
    /// </summary>
    /// <param name="imageUrl">The image URL.</param>
    /// <param name="imageUrlGenerator">
    ///     The generator that will process all the options and the image URL to return a full
    ///     image URLs with all processing options appended.
    /// </param>
    /// <param name="cropDataSet">The crop data set.</param>
    /// <param name="width">The width of the output image.</param>
    /// <param name="height">The height of the output image.</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 if there is one.
    /// </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="cacheBusterValue">
    ///     Add a serialized date of the last edit of the item to ensure client cache refresh when
    ///     updated.
    /// </param>
    /// <param name="furtherOptions">
    ///     These are any query string parameters (formatted as query strings) that the underlying image processing service
    ///     supports. For example:
    ///     <example><![CDATA[
    /// furtherOptions: "bgcolor=fff"
    /// ]]></example>
    /// </param>
    /// <returns>
    ///     The URL of the cropped image.
    /// </returns>
    public static string?GetCropUrl(
        this string imageUrl,
        IImageUrlGenerator imageUrlGenerator,
        ImageCropperValue?cropDataSet,
        int?width                       = null,
        int?height                      = null,
        string?cropAlias                = null,
        int?quality                     = null,
        ImageCropMode?imageCropMode     = null,
        ImageCropAnchor?imageCropAnchor = null,
        bool preferFocalPoint           = false,
        bool useCropDimensions          = false,
        string?cacheBusterValue         = null,
        string?furtherOptions           = null)
    {
        if (string.IsNullOrWhiteSpace(imageUrl))
        {
            return(null);
        }

        ImageUrlGenerationOptions options;

        if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null))
        {
            ImageCropperValue.ImageCropperCrop?crop = cropDataSet.GetCrop(cropAlias);

            // If a crop was specified, but not found, return null
            if (crop == null && !string.IsNullOrWhiteSpace(cropAlias))
            {
                return(null);
            }

            options = cropDataSet.GetCropBaseOptions(imageUrl, crop, preferFocalPoint || string.IsNullOrWhiteSpace(cropAlias));

            if (crop != null && useCropDimensions)
            {
                width  = crop.Width;
                height = crop.Height;
            }

            // Calculate missing dimension if a predefined crop has been specified, but has no coordinates
            if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null)
            {
                if (width != null && height == null)
                {
                    height = (int)MathF.Round(width.Value * ((float)crop.Height / crop.Width));
                }
                else if (width == null && height != null)
                {
                    width = (int)MathF.Round(height.Value * ((float)crop.Width / crop.Height));
                }
            }
        }
        else
        {
            options = new ImageUrlGenerationOptions(imageUrl)
            {
                ImageCropMode   = imageCropMode ?? ImageCropMode.Pad, // Not sure why we default to Pad
                ImageCropAnchor = imageCropAnchor,
            };
        }

        options.Quality          = quality;
        options.Width            = width;
        options.Height           = height;
        options.FurtherOptions   = furtherOptions;
        options.CacheBusterValue = cacheBusterValue;

        return(imageUrlGenerator.GetImageUrl(options));
    }