/// <summary>
        /// Generate transformation string.
        /// </summary>
        /// <returns>Generated transformation.</returns>
        public string GenerateThis()
        {
            string size = GetString(m_transformParams, "size");

            if (size != null)
            {
                string[] sizeComponents = size.Split("x".ToArray());
                m_transformParams.Add("width", sizeComponents[0]);
                m_transformParams.Add("height", sizeComponents[1]);
            }

            string width  = GetString(m_transformParams, "width");
            string height = GetString(m_transformParams, "height");

            if (m_htmlWidth == null)
            {
                m_htmlWidth = width;
            }

            if (m_htmlHeight == null)
            {
                m_htmlHeight = height;
            }

            bool hasLayer = !string.IsNullOrEmpty(GetString(m_transformParams, "overlay")) ||
                            !string.IsNullOrEmpty(GetString(m_transformParams, "underlay"));

            var crop  = GetString(m_transformParams, "crop");
            var angle = string.Join(".", GetStringArray(m_transformParams, "angle"));

            bool isResponsive;

            if (!bool.TryParse(GetString(m_transformParams, "responsive_width"), out isResponsive))
            {
                isResponsive = DefaultIsResponsive;
            }

            bool noHtmlSizes = hasLayer || !string.IsNullOrEmpty(angle) || crop == "fit" || crop == "limit";

            if (!string.IsNullOrEmpty(width) && (Expression.ValueContainsVariable(width) ||
                                                 width.IndexOf("auto", StringComparison.OrdinalIgnoreCase) != -1 ||
                                                 (float.TryParse(width, out var wResult) && wResult < 1) ||
                                                 noHtmlSizes ||
                                                 isResponsive))
            {
                m_htmlWidth = null;
            }

            if (!string.IsNullOrEmpty(height) && (Expression.ValueContainsVariable(height) ||
                                                  (float.TryParse(height, out var hResult) && hResult < 1) ||
                                                  noHtmlSizes ||
                                                  isResponsive))
            {
                m_htmlHeight = null;
            }

            string background = GetString(m_transformParams, "background");

            if (background != null)
            {
                background = background.Replace("^#", "rgb:");
            }

            string color = GetString(m_transformParams, "color");

            if (color != null)
            {
                color = color.Replace("^#", "rgb:");
            }

            List <string> transformations = GetStringArray(m_transformParams, "transformation").ToList();

            string namedTransformation = string.Join(".", transformations.ToArray());

            transformations = new List <string>();

            string flags = string.Join(".", GetStringArray(m_transformParams, "flags"));

            object obj = null;

            string startOffset = null;

            if (m_transformParams.TryGetValue("start_offset", out obj))
            {
                startOffset = NormAutoRangeValue(obj);
            }

            string endOffset = null;

            if (m_transformParams.TryGetValue("end_offset", out obj))
            {
                endOffset = NormRangeValue(obj);
            }

            if (m_transformParams.TryGetValue("offset", out obj))
            {
                var offset = SplitRange(m_transformParams["offset"]);
                if (offset != null && offset.Length == 2)
                {
                    startOffset = NormAutoRangeValue(offset[0]);
                    endOffset   = NormRangeValue(offset[1]);
                }
            }

            string duration = null;

            if (m_transformParams.TryGetValue("duration", out obj))
            {
                duration = NormRangeValue(obj);
            }

            string videoCodec = m_transformParams.TryGetValue("video_codec", out obj) ? ProcessVideoCodec(obj) : null;

            if (!m_transformParams.TryGetValue("dpr", out object dpr))
            {
                dpr = DefaultDpr;
            }

            var dprStr = ToString(dpr);

            if (!string.IsNullOrEmpty(dprStr))
            {
                if (dprStr.ToLower() == "auto")
                {
                    HiDpi = true;
                }
            }

            var parameters = new SortedList <string, string>();

            parameters.Add("a", Expression.Normalize(angle));
            parameters.Add("ar", Expression.Normalize(GetString(m_transformParams, "aspect_ratio")));
            parameters.Add("b", background);
            parameters.Add("c", crop);
            parameters.Add("co", color);
            parameters.Add("dpr", dprStr);
            parameters.Add("du", duration);
            parameters.Add("e", Expression.Normalize(GetString(m_transformParams, "effect")));
            parameters.Add("eo", endOffset);
            parameters.Add("fl", flags);
            var fnValue = GetString(m_transformParams, "custom_function") ??
                          GetString(m_transformParams, "custom_pre_function");

            parameters.Add("fn", fnValue);
            parameters.Add("h", Expression.Normalize(height));
            parameters.Add("o", Expression.Normalize(GetString(m_transformParams, "opacity")));
            parameters.Add("q", Expression.Normalize(GetString(m_transformParams, "quality")));
            parameters.Add("r", Expression.Normalize(GetString(m_transformParams, "radius")));
            parameters.Add("so", startOffset);
            parameters.Add("t", namedTransformation);
            parameters.Add("vc", videoCodec);
            parameters.Add("w", Expression.Normalize(width));
            parameters.Add("x", Expression.Normalize(GetString(m_transformParams, "x")));
            parameters.Add("y", Expression.Normalize(GetString(m_transformParams, "y")));
            parameters.Add("z", Expression.Normalize(GetString(m_transformParams, "zoom")));

            for (int i = 0; i < SimpleParams.Length; i += 2)
            {
                if (m_transformParams.TryGetValue(SimpleParams[i + 1], out obj))
                {
                    parameters.Add(SimpleParams[i], ToString(obj));
                }
            }

            List <string> components = new List <string>();

            string ifValue = GetString(m_transformParams, "if");

            if (!string.IsNullOrEmpty(ifValue))
            {
                components.Insert(0, string.Format("if_{0}", new Condition(ifValue).ToString()));
            }

            SortedSet <string> varParams = new SortedSet <string>();

            foreach (var key in m_transformParams.Keys)
            {
                if (Regex.IsMatch(key, Expression.VARIABLE_NAME_REGEX))
                {
                    varParams.Add($"{key}_{GetString(m_transformParams, key)}");
                }
            }

            if (varParams.Count > 0)
            {
                components.Add(string.Join(",", varParams));
            }

            string vars = m_transformParams.TryGetValue(VARIABLESPARAMKEY, out obj) && obj is Expression[] expressions
                ? ProcessVariables(expressions)
                : null;

            if (!string.IsNullOrEmpty(vars))
            {
                components.Add(string.Join(",", vars));
            }

            foreach (var param in parameters)
            {
                if (!string.IsNullOrEmpty(param.Value))
                {
                    components.Add(string.Format("{0}_{1}", param.Key, param.Value));
                }
            }

            string rawTransformation = GetString(m_transformParams, "raw_transformation");

            if (rawTransformation != null)
            {
                components.Add(rawTransformation);
            }

            if (components.Count > 0)
            {
                transformations.Add(string.Join(",", components.ToArray()));
            }

            if (isResponsive)
            {
                transformations.Add(ResponsiveWidthTransform.Generate());
            }

            if (width == "auto" || isResponsive)
            {
                IsResponsive = true;
            }

            return(string.Join("/", transformations.ToArray()));
        }
        public string GenerateThis()
        {
            string size = GetString(m_transformParams, "size");

            if (size != null)
            {
                string[] sizeComponents = size.Split("x".ToArray());
                m_transformParams.Add("width", sizeComponents[0]);
                m_transformParams.Add("height", sizeComponents[1]);
            }

            string width  = GetString(m_transformParams, "width");
            string height = GetString(m_transformParams, "height");

            if (m_htmlWidth == null)
            {
                m_htmlWidth = width;
            }

            if (m_htmlHeight == null)
            {
                m_htmlHeight = height;
            }

            bool hasLayer = !string.IsNullOrEmpty(GetString(m_transformParams, "overlay")) ||
                            !string.IsNullOrEmpty(GetString(m_transformParams, "underlay"));

            var crop  = GetString(m_transformParams, "crop");
            var angle = string.Join(".", GetStringArray(m_transformParams, "angle"));

            bool isResponsive = false;

            if (!bool.TryParse(GetString(m_transformParams, "responsive_width"), out isResponsive))
            {
                isResponsive = DefaultIsResponsive;
            }

            bool no_html_sizes = hasLayer || !String.IsNullOrEmpty(angle) || crop == "fit" || crop == "limit";

            if (width != null && (width == "auto" || Single.Parse(width, CultureInfo.InvariantCulture) < 1 || no_html_sizes || isResponsive))
            {
                m_htmlWidth = null;
            }
            if (height != null && (Single.Parse(height, CultureInfo.InvariantCulture) < 1 || no_html_sizes || isResponsive))
            {
                m_htmlHeight = null;
            }

            string background = GetString(m_transformParams, "background");

            if (background != null)
            {
                background = background.Replace("^#", "rgb:");
            }

            string color = GetString(m_transformParams, "color");

            if (color != null)
            {
                color = color.Replace("^#", "rgb:");
            }

            List <string> transformations = GetStringArray(m_transformParams, "transformation").ToList();

            string namedTransformation = string.Join(".", transformations.ToArray());

            transformations = new List <string>();

            string flags = string.Join(".", GetStringArray(m_transformParams, "flags"));

            object obj         = null;
            string startOffset = null;
            string endOffset   = null;

            if (m_transformParams.TryGetValue("start_offset", out obj))
            {
                startOffset = NormRangeValue(obj);
            }

            if (m_transformParams.TryGetValue("end_offset", out obj))
            {
                endOffset = NormRangeValue(obj);
            }

            if (m_transformParams.TryGetValue("offset", out obj))
            {
                var offset = SplitRange(m_transformParams["offset"]);
                if (offset != null && offset.Length == 2)
                {
                    startOffset = NormRangeValue(offset[0]);
                    endOffset   = NormRangeValue(offset[1]);
                }
            }

            var parameters = new SortedDictionary <string, string>();

            parameters.Add("w", width);
            parameters.Add("h", height);
            parameters.Add("t", namedTransformation);
            parameters.Add("c", crop);
            parameters.Add("b", background);
            parameters.Add("co", color);
            parameters.Add("a", angle);
            parameters.Add("fl", flags);
            parameters.Add("so", startOffset);
            parameters.Add("eo", endOffset);

            if (m_transformParams.TryGetValue("duration", out obj))
            {
                parameters.Add("du", NormRangeValue(obj));
            }

            ProcessVideoCodec(parameters, m_transformParams);

            for (int i = 0; i < SimpleParams.Length; i += 2)
            {
                if (m_transformParams.TryGetValue(SimpleParams[i + 1], out obj))
                {
                    parameters.Add(SimpleParams[i], ToString(obj));
                }
            }

            object dpr = null;

            if (!m_transformParams.TryGetValue("dpr", out dpr))
            {
                dpr = DefaultDpr;
            }

            var dprStr = ToString(dpr);

            if (dprStr != null)
            {
                if (dprStr.ToLower() == "auto")
                {
                    HiDpi = true;
                }

                parameters.Add("dpr", dprStr);
            }

            if (width == "auto" || isResponsive)
            {
                IsResponsive = true;
            }

            List <string> components = new List <string>();

            foreach (var param in parameters)
            {
                if (!string.IsNullOrEmpty(param.Value))
                {
                    components.Add(string.Format("{0}_{1}", param.Key, param.Value));
                }
            }

            string rawTransformation = GetString(m_transformParams, "raw_transformation");

            if (rawTransformation != null)
            {
                components.Add(rawTransformation);
            }

            string ifValue = GetString(m_transformParams, "if");

            if (!string.IsNullOrEmpty(ifValue))
            {
                components.Insert(0, string.Format("if_{0}", new Condition(ifValue).ToString()));
            }

            if (components.Count > 0)
            {
                transformations.Add(string.Join(",", components.ToArray()));
            }

            if (isResponsive)
            {
                transformations.Add(ResponsiveWidthTransform.Generate());
            }

            return(string.Join("/", transformations.ToArray()));
        }
Example #3
0
        public string GenerateThis()
        {
            var str1 = GetString(m_transformParams, "size");

            if (str1 != null)
            {
                var strArray = str1.Split("x".ToArray());
                m_transformParams.Add("width", strArray[0]);
                m_transformParams.Add("height", strArray[1]);
            }
            var s1 = GetString(m_transformParams, "width");
            var s2 = GetString(m_transformParams, "height");

            if (m_htmlWidth == null)
            {
                m_htmlWidth = s1;
            }
            if (m_htmlHeight == null)
            {
                m_htmlHeight = s2;
            }
            var flag1  = !string.IsNullOrEmpty(GetString(m_transformParams, "overlay")) || !string.IsNullOrEmpty(GetString(m_transformParams, "underlay"));
            var str2   = GetString(m_transformParams, "crop");
            var str3   = string.Join(".", GetStringArray(m_transformParams, "angle"));
            var result = false;

            if (!bool.TryParse(GetString(m_transformParams, "responsive_width"), out result))
            {
                result = DefaultIsResponsive;
            }
            var flag2 = flag1 || !string.IsNullOrEmpty(str3) || str2 == "fit" || str2 == "limit";

            if (s1 != null && (s1 == "auto" || float.Parse(s1, CultureInfo.InvariantCulture) < 1.0 || flag2 || result))
            {
                m_htmlWidth = null;
            }
            if (s2 != null && (float.Parse(s2, CultureInfo.InvariantCulture) < 1.0 || flag2 || result))
            {
                m_htmlHeight = null;
            }
            var str4 = GetString(m_transformParams, "background");

            if (str4 != null)
            {
                str4 = str4.Replace("^#", "rgb:");
            }
            var str5 = GetString(m_transformParams, "color");

            if (str5 != null)
            {
                str5 = str5.Replace("^#", "rgb:");
            }
            var    str6        = string.Join(".", GetStringArray(m_transformParams, "transformation").ToList().ToArray());
            var    stringList1 = new List <string>();
            var    str7        = string.Join(".", GetStringArray(m_transformParams, "flags"));
            object objectValue = null;
            string str8        = null;
            string str9        = null;

            if (m_transformParams.TryGetValue("start_offset", out objectValue))
            {
                str8 = NormRangeValue(objectValue);
            }
            if (m_transformParams.TryGetValue("end_offset", out objectValue))
            {
                str9 = NormRangeValue(objectValue);
            }
            if (m_transformParams.TryGetValue("offset", out objectValue))
            {
                var strArray = SplitRange(m_transformParams["offset"]);
                if (strArray != null && strArray.Length == 2)
                {
                    str8 = NormRangeValue(strArray[0]);
                    str9 = NormRangeValue(strArray[1]);
                }
            }
            var parameters = new SortedDictionary <string, string>();

            parameters.Add("w", s1);
            parameters.Add("h", s2);
            parameters.Add("t", str6);
            parameters.Add("c", str2);
            parameters.Add("b", str4);
            parameters.Add("co", str5);
            parameters.Add("a", str3);
            parameters.Add("fl", str7);
            parameters.Add("so", str8);
            parameters.Add("eo", str9);
            if (m_transformParams.TryGetValue("duration", out objectValue))
            {
                parameters.Add("du", NormRangeValue(objectValue));
            }
            ProcessVideoCodec(parameters, m_transformParams);
            var index = 0;

            while (index < SimpleParams.Length)
            {
                if (m_transformParams.TryGetValue(SimpleParams[index + 1], out objectValue))
                {
                    parameters.Add(SimpleParams[index], ToString(objectValue));
                }
                index += 2;
            }
            object obj = null;

            if (!m_transformParams.TryGetValue("dpr", out obj))
            {
                obj = DefaultDpr;
            }
            var str10 = ToString(obj);

            if (str10 != null)
            {
                if (str10.ToLower() == "auto")
                {
                    HiDpi = true;
                }
                parameters.Add("dpr", str10);
            }
            if (s1 == "auto" || result)
            {
                IsResponsive = true;
            }
            var stringList2 = new List <string>();

            foreach (var keyValuePair in parameters)
            {
                if (!string.IsNullOrEmpty(keyValuePair.Value))
                {
                    stringList2.Add(string.Format("{0}_{1}", keyValuePair.Key, keyValuePair.Value));
                }
            }
            var str11 = GetString(m_transformParams, "raw_transformation");

            if (str11 != null)
            {
                stringList2.Add(str11);
            }
            var condition = GetString(m_transformParams, "if");

            if (!string.IsNullOrEmpty(condition))
            {
                stringList2.Insert(0, string.Format("if_{0}", new Condition(condition).ToString()));
            }
            if (stringList2.Count > 0)
            {
                stringList1.Add(string.Join(",", stringList2.ToArray()));
            }
            if (result)
            {
                stringList1.Add(ResponsiveWidthTransform.Generate());
            }
            return(string.Join("/", stringList1.ToArray()));
        }