Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • hyde 8 posts 88 karma points
    Sep 16, 2016 @ 10:03
    hyde
    0

    Upload Image and cropper info Progrmatically

    Hi Guys,

    I am trying to move media from 7.2 to 7.4, but to a new instance. so basically what i did I used the code bellow to get the structure and the details of the images in xml from the 7.2 instance.

       umbraco.DataLayer.ISqlHelper SqlHelper = umbraco.BusinessLogic.Application.SqlHelper;
    
    int mediaId = -1;
    //Create an empty xmlDocument.
    System.Xml.Linq.XDocument xmlDocument = new System.Xml.Linq.XDocument(
            new System.Xml.Linq.XElement("Media")
        );
    
    if (mediaId == -1)
    {
        //If the mediaId is -1 loop through all the top media nodes.
        using (umbraco.DataLayer.IRecordsReader dr = SqlHelper.ExecuteReader("Select id from umbracoNode where nodeObjectType = @type And parentId = -1 order by sortOrder",
            SqlHelper.CreateParameter("@type", umbraco.cms.businesslogic.media.Media._objectType)))
        {
            while (dr.Read())
            {
                //Get the hierarchical xml from each topnode and add it to the xmlDocument.
                xmlDocument.Root.Add(System.Xml.Linq.XElement.Parse(umbraco.library.GetMedia(dr.GetInt("id"), true).Current.OuterXml));
            }
        }
    }
    else
    {
        //Get the hierarchical xml from the media id and add it to the xmlDocument.
        xmlDocument.Root.Add(System.Xml.Linq.XElement.Parse(umbraco.library.GetMedia(mediaId, true).Current.OuterXml));
    }
    

    And it works just fine, but now i am trying to push things to the new instance of media I know i can create folders and images using the media service like below.

                var mediaImage = mediaService.CreateMedia("test4", 1152, "Image");
                mediaImage.SetValue("umbracoFile", test, fileStream);
                mediaService.Save(mediaImage);
    

    But the problem is i have loads of cropped images but i have no idea how to add them below is an xml sample

      <Folder id="1126" parentID="-1" level="1" creatorID="0" sortOrder="0" createDate="2014-09-29T16:22:37" updateDate="2015-05-12T16:27:28" nodeName="Images" urlName="images" path="-1,1126" isDoc="" nodeType="1031" writerName="  Admin" writerID="0" version="3c3ccf09-e001-4fe5-8b1c-75a2da18d40c" template="0" nodeTypeAlias="Folder">
        <ImageCropper id="13081" parentID="1126" level="5" creatorID="0" sortOrder="4" createDate="2015-10-26T16:21:23" updateDate="2016-02-22T10:32:32" nodeName="Bla bla" urlName="test" path="-1,1126,1235,1378,1401,13081" isDoc="" nodeType="12390" writerName="  Admin" writerID="0" version="5860627c-f0f9-49e9-8114-3d52d3f93f03" template="0" nodeTypeAlias="ImageCropper">
                  <umbracoHeight>200</umbracoHeight>
                  <umbracoBytes>54244</umbracoBytes>
                  <umbracoExtension>jpg</umbracoExtension>
                  <umbracoFile>{ "focalPoint": { "left": 0.5, "top": 0.5 }, "src": "/media/392270/cropped-test.jpg", "crops": [ { "alias": "CTA Image", "width": 542, "height": 364, "coordinates": { "x1": 0.038364212277255755, "y1": 0, "x2": 0.0984410114844898, "y2": 0 } }, { "alias": "Wide CTA Image", "width": 960, "height": 320 }, { "alias": "Sidebar Image", "width": 400, "height": 333 }, { "alias": "Home Featured Videos Image", "width": 300, "height": 200, "coordinates": { "x1": 0.057971014492753624, "y1": 0, "x2": 0.072463768115942032, "y2": 0 } }, { "alias": "Support   Box Image", "width": 670, "height": 107 }, { "alias": "Council Landing Page Image", "width": 166, "height": 173 }, { "alias": "Trainee Committee Image", "width": 166, "height": 173 }, { "alias": "Masthead Image", "width": 1280, "height": 200 } ] }</umbracoFile>
               </ImageCropper>
               <ImageCropper id="13082" parentID="1401" level="5" creatorID="0" sortOrder="5" createDate="2015-10-26T16:26:25" updateDate="2016-02-22T10:32:51" nodeName="Getting into Medical School" urlName="getting-into-medical-school" path="-1,1126,1235,1378,1401,13082" isDoc="" nodeType="12390" writerName="  Admin" writerID="0" version="8fd4760d-fde4-42ff-9073-2d4170bd5fdf" template="0" nodeTypeAlias="ImageCropper">
                  <umbracoHeight>313</umbracoHeight>
                  <umbracoBytes>259343</umbracoBytes>
                  <umbracoExtension>jpg</umbracoExtension>
                  <umbracoFile>{ "focalPoint": { "left": 0.5, "top": 0.5 }, "src": "/media/392271/gh_rcs_250715_urgent_28-v2.jpg", "crops": [ { "alias": "CTA Image", "width": 542, "height": 364, "coordinates": { "x1": 0.042627479228088981, "y1": 0, "x2": 0.24691453363709465, "y2": 0 } }, { "alias": "Wide CTA Image", "width": 960, "height": 320 }, { "alias": "Sidebar Image", "width": 400, "height": 333 }, { "alias": "Home Featured Videos Image", "width": 300, "height": 200, "coordinates": { "x1": 0.042942073170731711, "y1": 0, "x2": 0.24135670731707312, "y2": 0 } }, { "alias": "Support   Box Image", "width": 670, "height": 107 }, { "alias": "Council Landing Page Image", "width": 166, "height": 173 }, { "alias": "Trainee Committee Image", "width": 166, "height": 173 }, { "alias": "Masthead Image", "width": 1280, "height": 200 } ] }</umbracoFile>
               </ImageCropper>
    

    as you can see the nodeTypeAlias is ImageCropper so chow can i upload these images or insert the cropper details in.

    is it possible ?

    thank you in advance

    hyde

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 16, 2016 @ 14:59
    Sven Geusens
    100

    I do not have much time right now, but maybe this bit will help you along. If you have any more questions I will reply later.

    private string GetCropDataString( IMedia image, decimal focalpointLeft = 0.5m, decimal focalpointTop = 0.5m)
        {
            var cropdata = new ImageCropDataSet();
            var focalpoint = new ImageCropFocalPoint
            {
                Left = focalpointLeft,
                Top = focalpointTop
            };
            cropdata.Src = Umbraco.TypedMedia(image.Id).Url;
            cropdata.FocalPoint = focalpoint;
            cropdata.Crops = new List<ImageCropData>();
            return cropdata.ToString();
        }
    

    Set the string representation of the result as the value of your propertie.

  • hyde 8 posts 88 karma points
    Sep 20, 2016 @ 11:44
    hyde
    0

    Hello sven thanks for your reply

    can you please explain to me how to set the cropper to the image. your code set new value but i can't understand what should i do with the return value.

    thank you

    hyde

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 20, 2016 @ 13:47
    Sven Geusens
    0

    I have not tested this since I am making a couple of presumptions on how your project is set up.

    Looking at the xml you posted, I presume that you have a Media Type with alias "ImageCropper" that has the same properties as Media Type "Image" but renders them differently.

    I have no idea how to save a filestream to disk using the umbraco path without calling mediaImage.SetValue("umbracoFile", test, fileStream);

    SO...

    Create the image like normal but with the correct media type

    var mediaImage = mediaService.CreateMedia("test4", 1152, "ImageCropper");
            mediaImage.SetValue("umbracoFile", test, fileStream);
            mediaService.Save(mediaImage);
    

    This will create a media item and upload the file in the correct folder.

    Next, get the crop representation of that saved file, change the property, and save the media item again.

    mediaImage.SetValue("umbracoFile", GetCropDataString(mediaImage));
    mediaService.Save(mediaImage);
    

    Note: I updated my previous post to reflect the usage of the code here.

  • hyde 8 posts 88 karma points
    Sep 20, 2016 @ 14:01
    hyde
    0

    I get this error No MediaType matching the passed in Alias: 'ImageCropper' was found which i have no idea why. i know File and Image aliases are fine but when i try ImageCropper it fail.

    any ideas ?

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 20, 2016 @ 14:50
    Sven Geusens
    0

    Because you probably still have to create the media type in your new instance. Look in the developer section on your old instance for the media type if my presumptions are right, it should be there.

  • hyde 8 posts 88 karma points
    Sep 20, 2016 @ 15:58
    hyde
    0

    I just checked the developer section and the type is there Umbraco.ImageCropper . I am not sure what i am doing any idea where should i look for more clues.

    thank you

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 20, 2016 @ 16:34
    Sven Geusens
    0

    And you used the alias of that type in the media creation?

  • hyde 8 posts 88 karma points
    Sep 21, 2016 @ 10:04
    hyde
    0

    Hi Sven,

    still not working getting the same error. anymore ideas ?!!!

  • hyde 8 posts 88 karma points
    Sep 21, 2016 @ 15:04
    hyde
    0

    My apologies you are correct. i was missing the type in the media types. thank you for the help.

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 22, 2016 @ 07:48
    Sven Geusens
    0

    Glad to hear you got it to work.

  • hyde 8 posts 88 karma points
    Sep 20, 2016 @ 16:38
    hyde
    0

    Yes but without the umbraco. Just the imagecropper i will try the whole word tomorrow and tell you how it goes. And i will pass you some of thr code. Maybe i am doing something obviously wrong.

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft