protected override ValidatorResult Evaluate()

in src/DeloitteDigital.Atlas/Validators/ImageWidthHeightValidator.cs [78:121]


        protected override ValidatorResult Evaluate()
        {
            int minWidth = MainUtil.GetInt(Parameters["MinWidth"], 0);
            int minHeight = MainUtil.GetInt(Parameters["MinHeight"], 0);
            int maxWidth = MainUtil.GetInt(Parameters["MaxWidth"], int.MaxValue);
            int maxHeight = MainUtil.GetInt(Parameters["MaxHeight"], int.MaxValue);
            float aspectRatio = MainUtil.GetFloat(Parameters["AspectRatio"], 0.00f);

            if (ItemUri != null)
            {
                if (MediaItem == null)
                    return ValidatorResult.Valid;

                int width = MainUtil.GetInt(MediaItem.InnerItem["Width"], 0);
                int height = MainUtil.GetInt(MediaItem.InnerItem["Height"], 0);

                if (minWidth == maxWidth && minHeight == maxHeight && (width != minWidth || height != minHeight))
                    return GetResult("The image referenced in the Image field \"{0}\" does not match the size requirements. Image needs to be exactly {1}x{2} but is {3}x{4}",
                                     GetField().DisplayName, minWidth.ToString(), minHeight.ToString(), width.ToString(),
                                     height.ToString());

                if (aspectRatio != 0.00f && height != 0 && (Math.Round((double)width / height, 2) != Math.Round(aspectRatio, 2)))
                    return GetResult("The image referenced in the Image field \"{0}\" has an invalid aspect ratio. The aspect ratio needs to be {1} but is {2}.",
                                     GetField().DisplayName, Math.Round(aspectRatio, 2).ToString(),
                                     (Math.Round((double)width / height, 2).ToString()));

                if (width < minWidth)
                    return GetResult("The image referenced in the Image field \"{0}\" is too small. The width needs to be at least {1} pixels but is {2}.",
                                     GetField().DisplayName, minWidth.ToString(), width.ToString());

                if (height < minHeight)
                    return GetResult("The image referenced in the Image field \"{0}\" is too small. The height needs to be at least {1} pixels but is {2}.",
                                     GetField().DisplayName, minHeight.ToString(), height.ToString());

                if (width > maxWidth)
                    return GetResult("The image referenced in the Image field \"{0}\" is too big. The width needs to at most {1} pixels but is {2}.",
                                     GetField().DisplayName, maxWidth.ToString(), width.ToString());

                if (height > maxHeight)
                    return GetResult("The image referenced in the Image field \"{0}\" is too big. The height needs to be at most {1} pixels but is {2}.",
                                     GetField().DisplayName, maxHeight.ToString(), height.ToString());
            }
            return ValidatorResult.Valid;
        }