Example #1
0
        public string Xml(Config config, ImageInfo imageInfo)
        {
            XmlDocument doc = createBaseXmlDocument();
            XmlNode root = doc.DocumentElement;

            if (root == null) return null;

            XmlNode dateStampNode = doc.CreateNode(XmlNodeType.Attribute, "date", null);
            dateStampNode.Value = imageInfo.DateStamp.ToString("s");
            root.Attributes.SetNamedItem(dateStampNode);

            for (int i = 0; i < data.Count; i++)
            {
                Crop crop = (Crop) data[i];
                Preset preset = (Preset) config.presets[i];

                XmlNode newNode = doc.CreateElement("crop");

                XmlNode nameNode = doc.CreateNode(XmlNodeType.Attribute, "name", null);
                nameNode.Value = preset.Name;
                newNode.Attributes.SetNamedItem(nameNode);

                XmlNode xNode = doc.CreateNode(XmlNodeType.Attribute, "x", null);
                xNode.Value = crop.X.ToString();
                newNode.Attributes.SetNamedItem(xNode);

                XmlNode yNode = doc.CreateNode(XmlNodeType.Attribute, "y", null);
                yNode.Value = crop.Y.ToString();
                newNode.Attributes.SetNamedItem(yNode);

                XmlNode x2Node = doc.CreateNode(XmlNodeType.Attribute, "x2", null);
                x2Node.Value = crop.X2.ToString();
                newNode.Attributes.SetNamedItem(x2Node);

                XmlNode y2Node = doc.CreateNode(XmlNodeType.Attribute, "y2", null);
                y2Node.Value = crop.Y2.ToString();
                newNode.Attributes.SetNamedItem(y2Node);

                if (config.GenerateImages)
                {
                    XmlNode urlNode = doc.CreateNode(XmlNodeType.Attribute, "url", null);
                    urlNode.Value = String.Format("{0}/{1}_{2}.jpg",
                                                  imageInfo.RelativePath.Substring(0,
                                                                                   imageInfo.RelativePath.LastIndexOf(
                                                                                       '/')),
                                                  imageInfo.Name,
                                                  preset.Name);
                    newNode.Attributes.SetNamedItem(urlNode);
                }

                root.AppendChild(newNode);
            }

            return doc.InnerXml;
        }
Example #2
0
 /// <summary>
 /// Store data as string XML (overridden by ToXMl to store "real" XML
 /// XML format:
 /// <crops dateStamp="">
 ///     <crop name="" x="" y="" x2="" y2="" url="" />
 /// </crops>
 /// </summary>
 public void Save()
 {
     ImageInfo imageInfo = new ImageInfo(imgImage.ImageUrl);
     if (!imageInfo.Exists)
     {
         data.Value = "";
     }
     else
     {
         SaveData saveData = new SaveData(hdnRaw.Value);
         data.Value = saveData.Xml(config, imageInfo);
         imageInfo.GenerateThumbnails(saveData, config);
     }
 }
Example #3
0
        protected override void OnInit(EventArgs e)
        {
            this.ID = "ImageCropper";
            //base.OnInit(e);

            int propertyId = ((umbraco.cms.businesslogic.datatype.DefaultData)data).PropertyId;

            int currentDocumentId = ((umbraco.cms.businesslogic.datatype.DefaultData)data).NodeId;
            Property uploadProperty;

            // we need this ugly code because there's no way to use a base class
            CMSNode node = new CMSNode(currentDocumentId);
            if (node.nodeObjectType == Document._objectType)
            {
                uploadProperty = new Document(currentDocumentId).getProperty(config.UploadPropertyAlias);
            }
            else if (node.nodeObjectType == umbraco.cms.businesslogic.media.Media._objectType)
            {
                uploadProperty = new Media(currentDocumentId).getProperty(config.UploadPropertyAlias);
            }
            else if (node.nodeObjectType == Member._objectType)
            {
                uploadProperty = new Member(currentDocumentId).getProperty(config.UploadPropertyAlias);
            }
            else
            {
                throw new Exception("Unsupported Umbraco Node type for Image Cropper (only Document, Media and Members are supported.");
            }

            // upload property could be null here if the property wasn't found
            if (uploadProperty != null)
            {
                string relativeImagePath = uploadProperty.Value.ToString();

                ImageInfo imageInfo = new ImageInfo(relativeImagePath);

                imgImage.ImageUrl = relativeImagePath;
                imgImage.ID = String.Format("cropBox_{0}", propertyId);

                StringBuilder sbJson = new StringBuilder();
                StringBuilder sbRaw = new StringBuilder();

                try
                {
                    _xml = new XmlDocument();
                    _xml.LoadXml(data.Value.ToString());
                }
                catch
                {
                    _xml = createBaseXmlDocument();
                }

                sbJson.Append("{ \"current\": 0, \"crops\": [");

                for (int i = 0; i < config.presets.Count; i++)
                {
                    Preset preset = (Preset)config.presets[i];
                    Crop crop;

                    sbJson.Append("{\"name\":'" + preset.Name + "'");

                    sbJson.Append(",\"config\":{" +
                                  String.Format("\"targetWidth\":{0},\"targetHeight\":{1},\"keepAspect\":{2}",
                                                preset.TargetWidth, preset.TargetHeight,
                                                (preset.KeepAspect ? "true" : "false") + "}"));

                    if (imageInfo.Exists)
                    {
                        crop = preset.Fit(imageInfo);
                    }
                    else
                    {
                        crop.X = 0;
                        crop.Y = 0;
                        crop.X2 = preset.TargetWidth;
                        crop.Y2 = preset.TargetHeight;
                    }

                    // stored
                    if (_xml.DocumentElement != null && _xml.DocumentElement.ChildNodes.Count == config.presets.Count)
                    {
                        XmlNode xmlNode = _xml.DocumentElement.ChildNodes[i];

                        int xml_x = Convert.ToInt32(xmlNode.Attributes["x"].Value);
                        int xml_y = Convert.ToInt32(xmlNode.Attributes["y"].Value);
                        int xml_x2 = Convert.ToInt32(xmlNode.Attributes["x2"].Value);
                        int xml_y2 = Convert.ToInt32(xmlNode.Attributes["y2"].Value);

                        // only use xml values if image is the same and different from defaults (document is stored inbetween image upload and cropping)
                        //if (xml_x2 - xml_x != preset.TargetWidth || xml_y2 - xml_y != preset.TargetHeight)
                        //fileDate == imageInfo.DateStamp && (

                        if (crop.X != xml_x || crop.X2 != xml_x2 || crop.Y != xml_y || crop.Y2 != xml_y2)
                        {
                            crop.X = xml_x;
                            crop.Y = xml_y;
                            crop.X2 = xml_x2;
                            crop.Y2 = xml_y2;
                        }
                    }

                    sbJson.Append(",\"value\":{" + String.Format("\"x\":{0},\"y\":{1},\"x2\":{2},\"y2\":{3}", crop.X, crop.Y, crop.X2, crop.Y2) + "}}");
                    sbRaw.Append(String.Format("{0},{1},{2},{3}", crop.X, crop.Y, crop.X2, crop.Y2));

                    if (i < config.presets.Count - 1)
                    {
                        sbJson.Append(",");
                        sbRaw.Append(";");
                    }
                }

                sbJson.Append("]}");

                hdnJson.Value = sbJson.ToString();
                //hdnJson.ID = String.Format("json_{0}", propertyId);
                hdnRaw.Value = sbRaw.ToString();
                //hdnRaw.ID = String.Format("raw_{0}", propertyId);

                Controls.Add(imgImage);

                Controls.Add(hdnJson);
                Controls.Add(hdnRaw);

                string imageCropperInitScript =
                    "initImageCropper('" +
                    imgImage.ClientID + "', '" +
                    hdnJson.ClientID + "', '" +
                    hdnRaw.ClientID +
                    "');";

                Page.ClientScript.RegisterStartupScript(GetType(), ClientID + "_imageCropper", imageCropperInitScript, true);
                Page.ClientScript.RegisterClientScriptBlock(Resources.json2Script.GetType(), "json2Script", Resources.json2Script, true);
                Page.ClientScript.RegisterClientScriptBlock(Resources.jCropCSS.GetType(), "jCropCSS", Resources.jCropCSS);
                Page.ClientScript.RegisterClientScriptBlock(Resources.jCropScript.GetType(), "jCropScript", Resources.jCropScript, true);
                Page.ClientScript.RegisterClientScriptBlock(Resources.imageCropperScript.GetType(), "imageCropperScript", Resources.imageCropperScript, true);

            }

            base.OnInit(e);
        }
        void _generateButton_Click(object sender, EventArgs e)
        {
            Config config = new Config(Configuration);

            // get list of nodeids with this datatype
            using (IRecordsReader rdr = SqlHelper.ExecuteReader(
                "SELECT DISTINCT contentNodeId, " +
                "(SELECT Alias FROM cmsPropertyType WHERE Id = pd.propertyTypeId) AS propertyAlias " +
                "FROM cmsPropertyData pd " +
                "WHERE PropertyTypeId IN (SELECT Id FROM cmsPropertyType WHERE DataTypeId = " + _dataType.DataTypeDefinitionId + ")"))
            {
                while (rdr.Read())
                {
                    int documentId = rdr.GetInt("contentNodeId");
                    string propertyAlias = rdr.GetString("propertyAlias");

                    Document document = new Document(documentId);

                    Property cropProperty = document.getProperty(propertyAlias);
                    Property imageProperty = document.getProperty(config.UploadPropertyAlias);

                    if (cropProperty != null) // && cropProperty.Value.ToString() == ""
                    {
                        ImageInfo imageInfo = new ImageInfo(imageProperty.Value.ToString());

                        if (imageInfo.Exists)
                        {
                            SaveData saveData = new SaveData();

                            foreach (Preset preset in config.presets)
                            {
                                Crop crop = preset.Fit(imageInfo);
                                saveData.data.Add(crop);
                            }

                            //cropProperty.Value = saveData.Xml(config, imageInfo);

                            imageInfo.GenerateThumbnails(saveData, config);

                            if (document.Published)
                            {
                                //document.Publish(document.User);
                                //umbraco.library.UpdateDocumentCache(document.Id);
                            }
                            else
                            {
                                //document.Save();
                            }
                        }
                    }
                }
            }
        }
Example #5
0
        public Crop Fit(ImageInfo imageInfo)
        {
            Crop crop;

            if (Aspect >= imageInfo.Aspect)
            {
                // crop widest            hor    ver
                // relevant positioning: center top, center center, center bottom
                
                float h = ((float)imageInfo.Width / TargetWidth) * TargetHeight;
                
                crop.X = 0;
                crop.X2 = imageInfo.Width;

                switch(PositionV)
                {
                    case "T":
                        crop.Y = 0;
                        crop.Y2 = (int)h;
                        break;
                    case "B":
                        crop.Y = imageInfo.Height - (int)h;
                        crop.Y2 = imageInfo.Height;
                        break;
                    default: // CC
                        crop.Y = (int)(imageInfo.Height - h) / 2;
                        crop.Y2 = (int)(crop.Y + h);
                        break;
                }
            }
            else
            {

                // image widest
                // relevant positioning: left/right center, left/right top, left/right bottom
                
                float w = ((float)imageInfo.Height / TargetHeight) * TargetWidth;
                
                crop.Y = 0;
                crop.Y2 = imageInfo.Height;

                switch (PositionH)
                {
                    case "L":
                        crop.X = 0;
                        crop.X2 = (int)w;
                        break;
                    case "R":
                        crop.X = imageInfo.Width - (int)w;
                        crop.X2 = imageInfo.Width;
                        break;
                    default: // CC
                        crop.X = (int) (imageInfo.Width - w)/2;
                        crop.X2 = (int) (crop.X + w);
                        break;
                }

            }

            return crop;
        }