1 Reply Latest reply on Nov 25, 2013 9:30 PM by myDavey

    Help with grouping and placing images

    myDavey Level 1

      Hello,

      I have a page with many rectangles and I have to place images into them

      I have a list of all the images in a file which I loop through and place them one by one

      The problem is that some of them are doubled up - meaning, some rectangles need 2-3 images inside it.

      So, what I have to do is group the images together and then place the group into the rectangle.

       

      Can anyone advise / show how this could / should be done?

      How should I go about grouping them first?

      What I was doing was placing image by image, so when I come to a rectangle that already has an image, how do I group them to re-'place' them?

       

      All advice and help will be greatly appreciated!

      thanks,

      Davey

       

       

      Here is my placing code:

       

      var myInsFrame = myBoxes[myCell];
      if (myInsFrame.rectangles.length < 1) {
              myInsFrame.place(myPicFile, false);
              myInsFrame.fit(FitOptions.proportionally);
          }
          else {
              alert("There is already a picture on this day");
              // Here is where I am stuck
          }
      }
      
      
        • 1. Re: Help with grouping and placing images
          myDavey Level 1

          Hi!

          After many hours of research and coding..... I think we were successful!

          I am pretty new at Javascript, but not to scripting

          This code seems way to 'superfluous' to me - it seems like there are many steps which arent really needed... that is to the experts of course

          I am pretty sure it can be written much better...

          So, as my tired fingers continue typing, I want to request if anyone can comment on the code or change it in any way (as a boost in the learning process) - it would help me tremendously.

           

          Thanks in advance!

          Davey

           

          Here is the code

           

          function insertPicture(myCell, myBoxes, myPicFile) {
              
              var myInsFrame = myBoxes[myCell];     // the frame into which we want to paste the images
              var thisPage = myPages[myMonth-1];   // the page of the document in which we are currently working
              var isImg_1, isImg_2;
              
              isImg_1 = myInsFrame.graphics.length > 0;     // pictures can exist directly inside the frame 
              isImg_2 = myInsFrame.rectangles.length > 0;     // pictures can exist in a rectangle in the frame
              
              if (!isImg_1 && !isImg_2) {                   // if there is only one image, then place it
                  myInsFrame.place(myPicFile, false);
                  myInsFrame.fit(FitOptions.proportionally);
              }
              else {                                                   // if there is more than one image, the fun starts!
                  
                   // THE GOAL: get all images that are currently inside the frame, and the new picture to be placed - group them together and paste into myInsFrame 
          
                  // create new layer to hold all temp items
                  var myTempLayer = myDoc.layers.add({name:"Temp_Img_Layer"});
                      myTempLayer.move(LocationOptions.AT_END);
                      
                  // function returns array of all images within current frame
                  var myImgs = getImages(myInsFrame);
                  
                  // duplicate all images to new layer (duplicating, because to use move, we have to first ungroup the items...more steps)
                  for (var i=0; i<myImgs.length; i++) {
                      myImgs[i].duplicate(myTempLayer);
                  }
                  
                  // Get Bounds of the frame in which we will place the images
                  var myBounds = myInsFrame.geometricBounds;
                  var myWidth = myBounds[3]-myBounds[1];
                  var myHeight = myBounds[2]-myBounds[0];
                  
                  // Create temp frame to hold the new placed image
                  var tempBox = thisPage.textFrames.add({geometricBounds: [0,0,myHeight,myWidth]});
                  
                  // place image, duplicate its graphic, remove original with its frame
                  tempBox.place(myPicFile, false);
                  tempBox.select();        // this is needed, because "tempBox.graphics[0]" was returning an error - invalid object.
                  var newImg = myDoc.selection[0].graphics[0];
                  newImg.duplicate();
                  tempBox.remove();
                  
                  // Get all items on the temp layer, make new frames with adjusted size and position
                  var myTempImgs = myTempLayer.pageItems;
                  var amt = myTempImgs.length;
                  var tempImgFrames = [];
                  myW = myWidth/amt;
                  for (var i=0; i<amt; i++) {
                      var myX = myW*i;
                      tempImgFrames[i] = thisPage.textFrames.add({geometricBounds: [0,myX,myHeight,myW+myX]});
                  }
          
                  // Paste each page item into one of the previous created frames
                  for (var i=0; i<amt; i++) {
                      myTempImgs[i].select();
                      app.cut();
                      tempImgFrames[i].select();
                      app.pasteInto();
                      tempImgFrames[i].fit(FitOptions.FILL_PROPORTIONALLY);
                  }
                      
                  // Group items together so that they can together be placed into original frame
                  var myGroup = myDoc.groups.add(tempImgFrames);
                  myGroup.move(myDoc.layers.item("calendarPictures"));        //we have to move the group to the layer of the original frame... otherwise it cant paste into
                  myGroup.select();
                  app.cut();
                  myInsFrame.select();
                  app.pasteInto();
          
                  // remove layer, so that in next loop we wont get error message of "layer exists"
                  // technically we could have left this layer till the end of the entire process, but then we would have to clean up the page items each time
                  myTempLayer.remove();
              }
          }