Skip to main content
Known Participant
July 3, 2022
Question

Scaling

  • July 3, 2022
  • 2 replies
  • 7238 views

Hi all, can anyone please help me to retrieve a scaling value of  image in illustrator. How can we identify which scaling has been applied to the image using script. I would like to know how to check whether the image has been applied horizontal scale or vertical scale. Thank you. 

This topic has been closed for replies.

2 replies

m1b
Community Expert
Community Expert
July 3, 2022

Hi @Abpujar, please try this. - Mark

Edit 1: improved script.

Edit 2: updated script to better handle RasterItems which have flipped scale Y values. Still more work to fix fully.

Edit 3: now handles RasterItems and PlacedItems correctly (I think!).

 

 

/**
 * Get PlacedItem Scale And Rotation
 * for Adobe Illustrator
 *
 * by m1b
 * discussion: https://community.adobe.com/t5/illustrator-discussions/scaling/m-p/13050842
 *
 */


var doc = app.activeDocument,
    item = doc.selection[0],
    result = getLinkScaleAndRotation(item);

if (result)
    alert('Results:\nHorizontal scale: ' + result[0] + '%\nVertical scale: ' + result[1] + '%\nRotation: ' + result[2] + '°');



/**
 * Return a linked item's scale.
 * @author m1b
 * @param {PlacedItem|RasterItem} item - an Illustrator item
 * @returns {Array} [scaleX%, scaleY%, rotation°]
 */
function getLinkScaleAndRotation(item) {
    if (item == undefined)
        return;

    var m = item.matrix,
        rotatedAmount,
        unrotatedMatrix,
        scaledAmount;

    var flipPlacedItem = (item.typename == 'PlacedItem')
        ? 1
        : -1;

    try {
        rotatedAmount = item.tags.getByName('BBAccumRotation').value * 180 / Math.PI;
    } catch (error) {
        rotatedAmount = 0;
    }
    unrotatedMatrix = app.concatenateRotationMatrix(m, rotatedAmount * flipPlacedItem);
    scaledAmount = [unrotatedMatrix.mValueA * 100, unrotatedMatrix.mValueD * -100 * flipPlacedItem];

    return [round(scaledAmount[0]), round(scaledAmount[1]), round(rotatedAmount)];
}



/**
 * Rounds `n` to `places` decimal places.
 * @param {Number} n - the number to round
 * @param {Number} places - number of decimal places, can be negative
 * @returns {Number}
 */
function round(n, places) {
    var m = Math.pow(10, places || 4);
    return Math.round(n * m) / m;
}

 

 

 

AbpujarAuthor
Known Participant
July 4, 2022

Hi @m1b @thank you for the reply, I’m getting the scaling value but I need -ve or +ve value, for example if the image has been applied horizontal scale then the scaling value should be -ve or if the image has a vertical scale then the vertical scale value should be in -ve. If the image has not been applied any scaling then the scaling value should be in +ve. Thank you. 

AbpujarAuthor
Known Participant
July 4, 2022

Okay, can you post a small sample illustrator file that shows the error?

- Mark


var xScale = Math.sqrt(my_matrix.mValueA my_matrix.mValueA + my_matrix.mValueB my_matrix.mValueB); var yScale = Math.sqrt(my_matrix.mValueC my_matrix.mValueC + my_matrix.mValueD my_matrix.mValueD); xScale = xScale * 100; yScale = yScale * 100;

 

Here I’m getting scaling value , how can we identify whether the image has been flipped or not after applying scaling.if the image has been flipped horizontally then the horizontal scaling value should be -100. How can we calculate ?

 

Ton Frederiks
Community Expert
Community Expert
July 3, 2022

The Links panel gives scaling info.