コード例 #1
0
        /// <summary>
        /// Executes an HTTP POST to a specific path - which adds an item to the Shopify DB
        /// </summary>
        dynamic Post(string url, string json)
        {
            var result = ExecuteRequest(url, "POST", json);

            //the result will be a pile of JSON
            //deserialize it and return
            return(JsonHelper.Decode(result));
        }
コード例 #2
0
        /// <summary>
        /// Executes a PUT to Shopify - used for Updates
        /// </summary>
        dynamic Put(string id, string json)
        {
            //build the URL
            var url    = _baseUrl + this._objectType + "/" + id + ".json";
            var result = ExecuteRequest(url, "PUT", json);

            return(JsonHelper.Decode(result));
        }
コード例 #3
0
        /// <summary>
        /// Executes an HTTP POST - which adds an item to the Shopify DB
        /// </summary>
        dynamic Post(string json)
        {
            //build the URL
            var url    = _shopUrl + this._objectType + ".json";
            var result = ExecuteRequest(url, "POST", json);

            //the result will be a pile of JSON
            //deserialize it and return
            return(JsonHelper.Decode(result));
        }
コード例 #4
0
        /// <summary>
        /// This builds a query with the passed in named arguments - shopify.Products(collection_id:121212)
        /// </summary>
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var name = binder.Name.ToLower() + ".json";
            var url  = _baseUrl + name;

            //params?
            var info   = binder.CallInfo;
            var looper = 0;

            if (info.ArgumentNames.Count > 0)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    var argName = info.ArgumentNames[i].ToLower();
                    var val     = args[i];
                    //the ID is a singular call
                    //with a special format
                    if (argName == "id")
                    {
                        url = url.Replace(".json", "/" + val + ".json");
                    }
                    else
                    {
                        if (looper == 0)
                        {
                            url += "?";
                        }
                        else
                        {
                            url += "&";
                        }
                        url += string.Format("{0}={1}", argName, val);
                    }
                    looper++;
                }
            }
            var json = Send(url);

            result = JsonHelper.Decode(json);
            return(true);
        }
コード例 #5
0
        /// <summary>
        /// This mehtod generates a composite image that includes all layers so we have a full product thumbnail
        /// </summary>
        /// <param name="size"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public static byte[] Thumbnail(string size, string d)
        {
            Size thumbSize = new Size(107, 67);

            if (size == "medium")
            {
                thumbSize = new Size(160, 100);
            }
            else if (size == "large")
            {
                thumbSize = new Size(320, 200);
            }

            using (AtalaImage image = new AtalaImage(thumbSize.Width, thumbSize.Height, PixelFormat.Pixel32bppBgra, Color.Transparent))
            {
                string product = string.Empty;
                string view    = "default";

                #region Build layers from input json
                //expected format
                //[["productName","Bed"],["Size","Small"],["Top","NavyBlue"],["Side","NavyBlue"],["Piping","Pink"],["text",""],["banner","Oval"],["font","BigDog.TTF"],["textColor","000000"],["image1",""],["image2",""],["image3",""]]
                var      props             = JsonHelper.Decode(d);
                string[] skippedProperties = { "Size", "text", "banner", "font", "textColor", "image1", "image2", "image3" };
                var      layers            = new List <KeyValuePair <string, string> >();
                var      allProperties     = new List <KeyValuePair <string, string> >();
                foreach (var prop in props)
                {
                    string key   = prop[0];
                    string value = prop[1];
                    allProperties.Add(new KeyValuePair <string, string>(key, value));
                    if (skippedProperties.Contains(key))
                    {
                        continue;
                    }
                    if (key == "productName")
                    {
                        product = value;
                    }
                    else
                    {
                        layers.Add(new KeyValuePair <string, string>(key, value));
                    }
                }
                #endregion

                #region Draw Layers
                var g = image.GetGraphics();
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                #region Draw Base
                string basePath = HttpContext.Current.Server.MapPath(string.Format("~/AppContent/ProductImages/{0}/{1}/base.png", product, view));
                if (File.Exists(basePath))
                {
                    using (AtalaImage uploadImage = new AtalaImage(basePath))
                    {
                        g.DrawImage(uploadImage.ToBitmap(), new RectangleF(0, 0, thumbSize.Width, thumbSize.Height));
                    }
                }
                #endregion
                foreach (var layer in layers)
                {
                    string imgPath = HttpContext.Current.Server.MapPath(string.Format("~/AppContent/ProductImages/{0}/{1}/{2}-{3}.png", product, view, layer.Key, layer.Value));
                    using (AtalaImage uploadImage = new AtalaImage(imgPath))
                    {
                        g.DrawImage(uploadImage.ToBitmap(), new RectangleF(0, 0, thumbSize.Width, thumbSize.Height));
                    }
                }
                #endregion

                #region Draw Text
                //Get product properties
                //string[] skippedProperties = { "Size", "text", "banner", "font", "textColor", "image1", "image2", "image3" };
                var textProp = allProperties.Where(kvp => kvp.Key == "text").FirstOrDefault();
                if (!string.IsNullOrEmpty(textProp.Value))
                {
                    dynamic productSchema = ProductSchemaProvider.GetProductSchema(product);
                    var     defaultView   = productSchema.views[0];
                    var     textLayer     = ((IEnumerable)defaultView.layers).Cast <dynamic>().Where(l => l.displayType == "text").FirstOrDefault();
                    if (textLayer != null)
                    {
                        JToken defaults = (JToken)textLayer.defaults;

                        var colorProp  = allProperties.Where(kvp => kvp.Key == "textColor").FirstOrDefault();
                        var fontProp   = allProperties.Where(kvp => kvp.Key == "font").FirstOrDefault();
                        var bannerProp = allProperties.Where(kvp => kvp.Key == "banner").FirstOrDefault();
                        var image1Prop = allProperties.Where(kvp => kvp.Key == "image1").FirstOrDefault();

                        var placeholderImageLocation = "";

                        // TODO: Clean this up
                        if (image1Prop.Value != null)
                        {
                            if (product == "Frisbee")
                            {
                                placeholderImageLocation = "placeholder";
                            }
                            else
                            {
                                placeholderImageLocation = "placeholder2";
                            }
                        }

                        byte[] textImage = TextImage(defaults.GetValue <string>("drawMode", ""),
                                                     textProp.Value,
                                                     colorProp.Value != null ? colorProp.Value : defaults.GetValue <string>("textColor"),
                                                     defaults.GetValue <int>("Size", 40),
                                                     fontProp.Value != null ? fontProp.Value : defaults.GetValue <string>("font"),
                                                     defaults.GetValue <int>("x", 0),
                                                     defaults.GetValue <int>("y", 0),
                                                     defaults.GetValue <int>("h", 0),
                                                     defaults.GetValue <int>("w", 0),
                                                     defaults.GetValue <int>("r1", 0),
                                                     defaults.GetValue <int>("s2", 0),
                                                     defaults.GetValue <string>("align", ""),
                                                     defaults.GetValue <int>("x2", 320),
                                                     defaults.GetValue <int>("y2", 110),
                                                     defaults.GetValue <int>("h2", 260),
                                                     defaults.GetValue <int>("w2", 220),
                                                     defaults.GetValue <int>("r2", 0),
                                                     defaults.GetValue <int>("s2", 0),
                                                     bannerProp.Value != null ? bannerProp.Value : defaults.GetValue <string>("banner"),
                                                     image1Prop.Value != null ? placeholderImageLocation : string.Empty);
                        using (var textImageMemory = new MemoryStream(textImage))
                        {
                            using (AtalaImage uploadImage = new AtalaImage(textImageMemory))
                            {
                                g.DrawImage(uploadImage.ToBitmap(), new RectangleF(0, 0, thumbSize.Width, thumbSize.Height));
                            }
                        }
                    }
                }
                #endregion

                #region Save Image
                byte[] data = null;
                using (MemoryStream m = new MemoryStream())
                {
                    PngEncoder encoder = new PngEncoder();
                    encoder.Save(m, image, null);
                    data = m.ToArray();
                    return(data);
                }
                #endregion
            }
        }