Skip to main content
Participant
October 13, 2022
Answered

Script to resize a file PNG

  • October 13, 2022
  • 2 replies
  • 268 views

Good evening everyone:

My script, opens a PNG file and reads its contents. Then the script opens another file (Mockup_001.ai) and pastes the PNG file. The problem now is that I need this PNG image to be in a certain position and with a certain size. How to proceed? Follow the script...

Thanks for any suggestion.


var filePng = "C:\\Temp\\PNG.png";
var fileMockup = "C:\\Temp\\Mockup_001.ai";

var docPng=app.open(File(filePng));

var newItem;
var copiaPNG = app.activeDocument.selection;

mockup = app.open(File(fileMockup));

if (copiaPNG.length > 0) { 
for(var i=0; i<copiaPNG.length; i++){

copiaPNG[i].selected = false;

newItem = copiaPNG[i].duplicate(mockup, ElementPlacement.PLACEATBEGINNING);

}
}

This topic has been closed for replies.
Correct answer Charu Rajput

Hi @Alexandre - Alenac2001,

Adding more what @CarlosCanto  suggested,  Another way to resize the item can be using the scale factor

var newItem = app.selection[0];
var scaleFactor = 0.25;
newItem.resize(scaleFactor * 100, scaleFactor * 100, true);
newItem.position = new Array(0,0);

 

The above snippet, will resize the selected item by 25% and will position at x=0 and y=0.

2 replies

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 13, 2022

Hi @Alexandre - Alenac2001,

Adding more what @CarlosCanto  suggested,  Another way to resize the item can be using the scale factor

var newItem = app.selection[0];
var scaleFactor = 0.25;
newItem.resize(scaleFactor * 100, scaleFactor * 100, true);
newItem.position = new Array(0,0);

 

The above snippet, will resize the selected item by 25% and will position at x=0 and y=0.

Best regards
CarlosCanto
Community Expert
Community Expert
October 13, 2022

opening the png file and copying it to the mockup file is not very efficient. I would open the mockup file and "place" (import) the png file instead

 

 

var filePng = "C:\\Temp\\PNG.png";
var fileMockup = "C:\\Temp\\Mockup_001.ai";

var mockup = app.open(File(fileMockup));

var placedPng = mockup.placedItems.add();
placedPng.file = File(filePng);

// you need to figure out the right size and placement;
placedPng.width = 200;
placedPng.height = 300;
placedPng.position = [50, -30];