Skip to main content
dublove
Legend
April 2, 2026
Question

How to fit image into a specific area and specify the cropping direction

  • April 2, 2026
  • 1 reply
  • 13 views

I want the image to fit within the page margins, with any excess areas cropped from the top and right sides.

When the image is on the left page, there’s no problem.
When the image is on the right page, after running the script, only the frame fits the page layout, but the image itself disappears.

I’ve been trying to figure this out for a while and suspect it might be because the image extends too far to the right.

Am I using FitOptions incorrectly?
Or is there something wrong with the move command?

Thanks.

var doc = app.activeDocument;
var item = doc.selection[0];
var items = doc.selection;
var s = item;

s.geometricBounds = [40, 30, 247, 190];

s.fit(FitOptions.PROPORTIONALLY);
s.fit(FitOptions.FILL_PROPORTIONALLY);
alert('This part is still correct.');
var img = s.images[0];
var mcy = s.geometricBounds[2] - img.geometricBounds[2]
var mcx = s.geometricBounds[3] - img.geometricBounds[3]
img.move([0, 0], [-mcx, +mcy]);

 

    1 reply

    dublove
    dubloveAuthor
    Legend
    April 2, 2026

    Move 210 mm
    How was this value calculated?

    It seems that sometimes when the page height is greater than the width, one algorithm is used, while when the width is greater than the height, another algorithm is used.


    We may need to break this down further:

    var mcyT = s.geometricBounds[0] - img.geometricBounds[0]
    var mcyB = s.geometricBounds[2] - img.geometricBounds[2]
    var mcxL = s.geometricBounds[1] - img.geometricBounds[1]
    var mcxR = s.geometricBounds[3] - img.geometricBounds[3]