Beispiel #1
0
        private static IReadOnlyCollection <ImageSizeParam> ParseImageSizeParameters(HttpRequestMessage request)
        {
            NameValueCollection queryString = request.RequestUri.ParseQueryString();

            string[] sizeParams = queryString.GetValues("size");
            if (sizeParams == null || sizeParams.Length == 0)
            {
                throw new ArgumentException {
                          Data = { { "ValidationData", new { Query = new { Size = "Required" } } } }
                }
            }
            ;

            return(sizeParams.Select(sizeParam =>
            {
                if (!ImageSizeParam.TryParse(sizeParam, out ImageSizeParam size))
                {
                    throw new ArgumentException {
                        Data = { { "ValidationData", new { Query = new { Size = "Incorrect size format. It should be \"${width}x${height}q${quality}\"" } } } }
                    }
                }
                ;

                return size;
            }).ToList());
        }
Beispiel #2
0
        public void ShouldParseParameters(string input, int?width, int?height, int quality)
        {
            ImageSizeParam sizes = ImageSizeParam.Parse(input);

            Assert.Equal(sizes.Width, width);
            Assert.Equal(sizes.Height, height);
            Assert.Equal(sizes.Quality, quality);
        }
        public OutputImageParameters Resize(InputImageParameters inputImage, ImageSizeParam targetSize)
        {
            using (SKBitmap original = SKBitmap.Decode(inputImage.InputStream))
            {
                if (original == null)
                {
                    throw new ArgumentException {
                              Data = { { "ValidationData", new { Body = new { Image = "Something wrong with incoming image" } } } }
                    }
                }
                ;

                return(Resize(original, inputImage, targetSize));
            }
        }
        private OutputImageParameters Resize(SKBitmap original, InputImageParameters inputImage, ImageSizeParam targetSize)
        {
            OutputImageParameters outputImage = CalculateSize(original.Width, original.Height, targetSize, inputImage.MinimumDifference);

            ApplyResize(original, inputImage, outputImage);

            return(outputImage);
        }
        private OutputImageParameters CalculateSize(int originalWidth, int originalHeight, ImageSizeParam targetSize, double minimumDifference)
        {
            int width;
            int height;

            if (targetSize.Width.HasValue || targetSize.Height.HasValue)
            {
                int    widthDifference  = originalWidth - (targetSize.Width ?? (int.MinValue / 2));
                int    heightDifference = originalHeight - (targetSize.Height ?? (int.MinValue / 2));
                double difference       = Math.Min((double)widthDifference / originalWidth, (double)heightDifference / originalHeight);
                if (difference < minimumDifference)
                {
                    return(OutputImageParameters.NotResized);
                }

                if (widthDifference < heightDifference)
                {
                    width  = targetSize.Width.Value;
                    height = (int)Math.Round((double)originalHeight * width / originalWidth);
                }
                else
                {
                    height = targetSize.Height.Value;
                    width  = (int)Math.Round((double)originalWidth * height / originalHeight);
                }
            }
            else
            {
                width  = originalWidth;
                height = originalHeight;
            }

            return(new OutputImageParameters(width, height, targetSize.Quality));
        }
Beispiel #6
0
        public void ShouldConvertToCorrectString(int?width, int?height, int quality, string output)
        {
            ImageSizeParam size = new ImageSizeParam(width, height, quality);

            Assert.Equal(size.ToString(), output);
        }
Beispiel #7
0
        public void ShouldFailForWrongParameters(string input)
        {
            bool parsed = ImageSizeParam.TryParse(input, out ImageSizeParam size);

            Assert.False(parsed);
        }