12 Replies Latest reply on Sep 6, 2011 2:53 AM by Corwin86

    Crop an Image object selected by the user

    Corwin86

      Hi,

       

      perhaps it is a simple question, but I can not figure out how to resolve the situation...

       

      I need (with a Javascript script) to crop an image selected by the user.

       

      I'm trying to use geometricBounds, with no luck.

       

      Thanks for the help and best regards!

        • 1. Re: Crop an Image object selected by the user
          OlivierBerquin Level 1

          Hi Corwin86,

           

          What do you mean by "crop an image"?
          You want the frame containing the image is smaller (or larger)?

          Or do you want the image itself smaller (or larger)?

           

           

          Oli.

          • 2. Re: Crop an Image object selected by the user
            Corwin86 Level 1

            Hi,

             

            I want the image itself smaller (or larger).

             

            Ex: the user select only a small portion of the image, then the script run and "crop" the image inside the selection. The new image is, then, only the cropped part.

            • 3. Re: Crop an Image object selected by the user
              Andreas Jansson Level 2

              How can the user select only a small portion of an image? Are you getting coordinates for a selection, into this script?

               

              Otherwise, this would be very close to the normal manual resizing of an image in InDesign, inside its container (frame). You could look at the image container as a viewport, displaying only a small portion of an image.

               

              Best regards,

              A.J.

              • 4. Re: Crop an Image object selected by the user
                Corwin86 Level 1
                function(){return A.apply(null,[this].concat($A(arguments)))}

                Andreas Jansson wrote:

                 

                How can the user select only a small portion of an image? Are you getting coordinates for a selection, into this script?

                 

                Otherwise, this would be very close to the normal manual resizing of an image in InDesign, inside its container (frame). You could look at the image container as a viewport, displaying only a small portion of an image.

                 

                Best regards,

                A.J.

                 

                Yes, I think I have to obtain the coordinates of the selection. The user simply select the "new" image with the mouse, then use the script to "crop" that.

                 

                How can I obtain the coordinates of the viewable "new" image? And then make the "crop"?

                 

                Thanks.

                • 5. Re: Crop an Image object selected by the user
                  Andreas Jansson Level 2

                  If the user is working in InDesign, the whole image will be selected. I don't see how you can select a portion of it, unless you are reflecting a selection made in another program, such as a web application, communicating with InDesign (Server).

                   

                  Do you mean to really crop the physical image, to get a smaller file size or something? I believe you would have to use another program for that.

                   

                  I initially thought you meant to position the image within its container (and perhaps you do mean that), so that just a certain portion was made visible, by zooming in or out (but the rest of the image still there, but outside the "viewport") - that's the way InDesign work.

                   

                  Have a look at the screen-shot below. There is a selection rectangle, from the mouse, but it can not be "started" inside the image (as you see its left top corner is in the white area outside the image). Also: its coordinates can not be read out easily (as far as I know) and it disappears as soon as you release the mouse button..

                   

                  Capture.JPG

                   

                  This second image, below shows the same image after a manual resize.... wouldn't that be as easy for your user to do, just resize the image within its container? Or would you like your script to physically get rid of the (invisible) image area between the visible image part and the brown/green(?) selection, that you see on the screen shot below?

                  Capture2.JPG

                   

                  Please explain the purpose better, and someone might be able to helt you out.

                   

                  Best regards,

                  Andreas

                  1 person found this helpful
                  • 6. Re: Crop an Image object selected by the user
                    Corwin86 Level 1

                    Yes, I mean your second option

                     

                     

                    I would you like my script to physically get rid of the (invisible) image area between the visible image part and the brown/green(?) selection, when the user resize the image.

                    • 7. Re: Crop an Image object selected by the user
                      Andreas Jansson Level 2

                      InDesign is not capable of that. You would need to use an image component of some sort to do that kind of image editing.

                       

                      Best regards,

                      Andreas

                      • 8. Re: Crop an Image object selected by the user
                        La0s

                        Hello, take a look at these:

                         

                        Crop & Resize (Free) by Oleg Butrin
                        http://www.szam5.com/cropresize-en.html

                         

                        Transform Images to 100% (Not free) and others by Dmitriy Lapayev
                        https://sites.google.com/a/lapay.biz/www/

                        1 person found this helpful
                        • 9. Re: Crop an Image object selected by the user
                          Andreas Jansson Level 2

                          Ah, yes you are right!

                           

                          InDesign is not capable of that.

                           

                          It's quite possible. Sorry.

                          • 10. Re: Crop an Image object selected by the user
                            La0s Level 1

                            You are right Andreas.

                            I forgot to mention that these samples are working by letting InDesign use PhotoShop for the actual image processing.

                             

                            Best regards

                            • 11. Re: Crop an Image object selected by the user
                              Andreas Jansson Level 2

                              Just remembered that I do this kind of stuff in some of my scripts as well... Really sorry for missleading you.

                               

                              Select a rectangle with an image inside, and play around with a script like the one below, and I'm sure you will be able to adapt it for your needs:

                              // Test file path
                              jpgFileName = 'c:/temp/test.jpg';
                              
                              // Export the visible area of a selected image container (errors will occur unless valid object)
                              var newFile = app.selection[0].exportFile(format=ExportFormat.JPG , to=jpgFileName);
                              
                              // Relink the selected image to the exported, new image
                              app.selection[0].images[0].itemLink.relink(new File(jpgFileName));
                              
                              // Fit the newly linked image proportionally inside the selected object.
                              app.selection[0].fit(FitOptions.FILL_PROPORTIONALLY);
                              
                              

                              You might want to use settings like these as well:

                              //app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
                              //app.jpegExportPreferences.exportResolution = 72;

                               

                              Best regards,

                              Andreas

                               

                              PS. The "InDesign is not capable of that" was me quoting myself, and there was a strikethrough effect on that whole sentence, which did not display... DS.

                               

                              PPS. The image quality will of course deteriorate if you export to jpg. DS.

                              • 12. Re: Crop an Image object selected by the user
                                Corwin86 Level 1

                                Thanks everyone for the answers!