예제 #1
0
        /// <summary>
        /// Builds a url to a contentful image using the specified properties.
        /// </summary>
        /// <returns>The url.</returns>
        public async Task <string> BuildUrl()
        {
            if (Asset != null)
            {
                Url = Asset.File?.Url;
                if (string.IsNullOrEmpty(Url))
                {
                    AssetId = Asset.SystemProperties?.Id;
                }
            }

            if (string.IsNullOrEmpty(Url))
            {
                var asset = await _client.GetAssetAsync(AssetId, "");

                Url = asset.File.Url;
            }

            var queryBuilder = new ImageUrlBuilder();

            if (Width > 0)
            {
                queryBuilder.SetWidth(Width);
            }

            if (Height > 0)
            {
                queryBuilder.SetHeight(Height);
            }

            if (JpgQuality.HasValue)
            {
                queryBuilder.SetJpgQuality(JpgQuality.Value);
            }

            if (CornerRadius.HasValue)
            {
                queryBuilder.SetCornerRadius(CornerRadius.Value);
            }

            if (!string.IsNullOrEmpty(BackgroundColor))
            {
                queryBuilder.SetBackgroundColor(BackgroundColor);
            }

            queryBuilder.SetFocusArea(FocusArea);
            queryBuilder.SetResizingBehaviour(ResizeBehaviour);
            queryBuilder.SetFormat(Format);

            if (ProgressiveJpg)
            {
                queryBuilder.UseProgressiveJpg();
            }

            return($"{Url}{queryBuilder.Build()}");
        }
        public void NewUnusedImageBuilderShouldReturnEmptyString()
        {
            //Arrange
            var builder = new ImageUrlBuilder();
            //Act
            var result = builder.Build();

            //Assert
            Assert.Equal(string.Empty, result);
        }
예제 #3
0
        /// <summary>
        /// Executes the taghelper.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (string.IsNullOrEmpty(Url))
            {
                var asset = await _client.GetAssetAsync(AssetId, "");

                Url = asset.File.Url;
            }

            var queryBuilder = new ImageUrlBuilder();

            if (Width > 0)
            {
                queryBuilder.SetWidth(Width);
            }

            if (Height > 0)
            {
                queryBuilder.SetHeight(Height);
            }

            if (JpgQuality.HasValue)
            {
                queryBuilder.SetJpgQuality(JpgQuality.Value);
            }

            if (CornerRadius.HasValue)
            {
                queryBuilder.SetCornerRadius(CornerRadius.Value);
            }

            if (!string.IsNullOrEmpty(BackgroundColor))
            {
                queryBuilder.SetBackgroundColor(BackgroundColor);
            }

            queryBuilder.SetFocusArea(FocusArea);
            queryBuilder.SetResizingBehaviour(ResizeBehaviour);
            queryBuilder.SetFormat(Format);

            if (ProgressiveJpg)
            {
                queryBuilder.UseProgressiveJpg();
            }

            output.TagName = "img";
            output.Attributes.Add("src", Url + queryBuilder.Build());
        }
예제 #4
0
        /// <summary>
        /// Builds a url to a contentful image using the specified properties.
        /// </summary>
        /// <returns>The url.</returns>
        public async Task <string> BuildUrl()
        {
            if (Asset != null)
            {
                Url = Asset.File?.Url;
                if (string.IsNullOrEmpty(Url))
                {
                    AssetId = Asset.SystemProperties?.Id;
                }
            }

            if (string.IsNullOrEmpty(Url))
            {
                Asset = await _client.GetAsset(AssetId, "");

                Url = Asset.File.Url;
            }

            var contentType = Asset?.File?.ContentType;

            if (contentType == null && !string.IsNullOrEmpty(Url))
            {
                contentType = Url.ToLower().EndsWith(".jpg") || Url.ToLower().EndsWith(".jpeg") ? "image/jpeg" : "";
            }

            var isJpg = (contentType?.ToLower() == "image/jpeg" && Format == ImageFormat.Default) || Format == ImageFormat.Jpg;

            var queryBuilder = new ImageUrlBuilder();

            if (Width > 0)
            {
                queryBuilder.SetWidth(Width);
            }

            if (Height > 0)
            {
                queryBuilder.SetHeight(Height);
            }

            if (JpgQuality.HasValue && isJpg)
            {
                queryBuilder.SetJpgQuality(JpgQuality.Value);
            }

            if (CornerRadius.HasValue)
            {
                queryBuilder.SetCornerRadius(CornerRadius.Value);
            }

            if (!string.IsNullOrEmpty(BackgroundColor))
            {
                queryBuilder.SetBackgroundColor(BackgroundColor);
            }

            queryBuilder.SetFocusArea(FocusArea);
            queryBuilder.SetResizingBehaviour(ResizeBehaviour);
            queryBuilder.SetFormat(Format);

            if (ProgressiveJpg && isJpg)
            {
                queryBuilder.UseProgressiveJpg();
            }

            return($"{Url}{queryBuilder.Build()}");
        }