• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
6

Resize Image script

Enthusiast ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

I have several images in my document. My document width is 80mm.

 

I have lots of images to be placed.

 

I have created an object style which resizes the image frame to 80mm and fit the content proportionally.

Issue is that there is no option in object style to resize it proportionally height wise.

 

Is there any script which takes the dimensions of the image and then size is widthwise to 80mm and changes its height proportionally ?

 

Thanks

TOPICS
How to , Scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Oct 08, 2023 Oct 08, 2023

This sets the image frame to the page margins. To use doScript you have to wrap your code in a function and call the function in the doScript method. For getting and setting styles I call a separate function that checks if the style I’m creating already exists—makeObjStyle():

 

app.doScript(fitImage, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fit Image');



function fitImage(){
    app.activeDocument.zeroPoint = [0,0]; 

    //makes a new style or uses the picture style if 
...

Votes

Translate

Translate
Community Expert , Oct 08, 2023 Oct 08, 2023

Try adding this at the end:

 

frame.move([m,m]);

Votes

Translate

Translate
Enthusiast ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

I have written this script which seems to be working. I need tips to make this script better. Like I have hardcoded 80mm as my final width in the code. How to make it more dynamic?

var frame = app.activeDocument.selection[0];
var b = frame.geometricBounds;

//Record Original width and height
var origWidth = b[3]-b[1];
var origHeight = b[2]-b[0];

//Find the percentage change
//80mm is the new width

percentChange = ((80-origWidth)/origWidth)*100;

wChange = origWidth + (origWidth*(percentChange/100));
hChange = origHeight + (origHeight*(percentChange/100));


// Resize the frame
    frame.geometricBounds = [
        frame.geometricBounds[0], // Top coordinate
        frame.geometricBounds[1], // Left coordinate      
		frame.geometricBounds[0] + hChange, // Bottom coordinate
        frame.geometricBounds[1] + wChange // Right coordinate
    ];

frame.fit(FitOptions.PROPORTIONALLY);

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

My document width is 80mm.

 

Hi @shahidr100 , If you are trying to set the frame to fill your document page, you can get the parent page’s bounds rather than hard coding a number. Something like this:

 

var frame = app.activeDocument.selection[0];
//set the frame’s bounds to match its parent page bounds
frame.geometricBounds = frame.parentPage.bounds
frame.fit(FitOptions.PROPORTIONALLY);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

Hi Rob
Thanks for the reply.

Actually I am working on a long document with letter-half size.
I have given a margin of .75" all sides. Effective size for text to flow is 4 inches.

Text flow is fine. Some topics require images in between the text.

I want to place the image and run the script to resize its width to 4" and anchor it or use inline anchor.

The script will also apply an object style named "picture".

I am using this code to achieve that:

//Apply an object style to the selection
frame.applyObjectStyle(app.activeDocument.objectStyles.itemByName("picture"));

I have used your code. Its maximizing the image to the document size. I want it to be exactly the margin size. 4" in this case.

Secondly, I wanted to apply a single undo system using the doScript method which is not working.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

This sets the image frame to the page margins. To use doScript you have to wrap your code in a function and call the function in the doScript method. For getting and setting styles I call a separate function that checks if the style I’m creating already exists—makeObjStyle():

 

app.doScript(fitImage, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fit Image');



function fitImage(){
    app.activeDocument.zeroPoint = [0,0]; 

    //makes a new style or uses the picture style if it already exists
    var oStyle = makeObjStyle(app.activeDocument, "picture")
    
    //a selected frame
    var frame = app.activeDocument.selection[0];

    //gets the page bounds and the top margin assumes margins are equal
    var pb = frame.parentPage.bounds;
    var m = frame.parentPage.marginPreferences.top;

    //set the frame’s bounds to the margins
    frame.geometricBounds = [pb[0]+m, pb[1]+m, pb[2]-m, pb[3]-m];

    //fit the image and then the frame to the image
    frame.fit(FitOptions.PROPORTIONALLY);
    frame.fit(FitOptions.FRAME_TO_CONTENT);

    frame.appliedObjectStyle = oStyle;
}


/**
* Makes a new named ObjectStyle or returns an existing style 
* @ param the document to add the style to 
* @ param style name 
* @ return the new object style 
*/
function makeObjStyle(d, n){
    if (d.objectStyles.itemByName(n).isValid) {
        return d.objectStyles.itemByName(n);
    } else {
        return d.objectStyles.add({name:n});
    }
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

@rob day Thank you so much. It worked.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

@rob day Hi Rob

 

Just one issue which I noticed. Nothing major. I am attaching 3 photos. Before, after and Should_be.

There should not be any space before the "After" image. My final result should be like  "should_be" image.

Thanks

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

Try adding this at the end:

 

frame.move([m,m]);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 08, 2023 Oct 08, 2023

Copy link to clipboard

Copied

Thanks. It worked.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 17, 2024 Nov 17, 2024

Copy link to clipboard

Copied

Hi @shahidr100 @rob day 

What was the final script?
Can you share a complete one?

Can your script make any image scale isometrically to fit the column width, or typearea width?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 27, 2024 Nov 27, 2024

Copy link to clipboard

Copied

@dublove Sorry for late response.

 

Here is the final script.

 

app.doScript(fitImage, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fit Image');

 

function fitImage(){
app.activeDocument.zeroPoint = [0,0];

//makes a new style or uses the picture style if it already exists
var oStyle = makeObjStyle(app.activeDocument, "picture")

//a selected frame
var frame = app.activeDocument.selection[0];

//gets the page bounds and the top margin assumes margins are equal
var pb = frame.parentPage.bounds;
var m = frame.parentPage.marginPreferences.top;

//set the frame’s bounds to the margins
frame.geometricBounds = [pb[0]+m, pb[1]+m, pb[2]-m, pb[3]-m];

//fit the image and then the frame to the image
frame.fit(FitOptions.PROPORTIONALLY);
frame.fit(FitOptions.FRAME_TO_CONTENT);

frame.appliedObjectStyle = oStyle;
}


/**
* Makes a new named ObjectStyle or returns an existing style
* @ param the document to add the style to
* @ param style name
* @ return the new object style
*/
function makeObjStyle(d, n){
if (d.objectStyles.itemByName(n).isValid) {
return d.objectStyles.itemByName(n);
} else {
return d.objectStyles.add({name:n});
}
}

 

 

Isometric scaling is not available in the script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

LATEST

How to make an image isometric scaling  with width,

1.  Fit   to columns

2. Fit to  margins

3. Fit to typearea

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines