layer.resize
Copy link to clipboard
Copied
I'm writing a script to Trim an Image record the current width, calculate new width and increase in the image. Here is the code below, but have issues in the layer.resize. Anyone know, whats the problem?
// 1° Determinar pasta fonte das imagens
var sourceFolder = Folder.selectDialog("Select the source folder");
// 2° Determinar pasta no qual receberá novas imagens
var destinationFolder = Folder.selectDialog("Select the destination folder");
// Get all files in the source folder
var files = sourceFolder.getFiles();
// Loop over all files
for (var i = 0; i < files.length; i++) {
// Open the file
var doc = app.open(files[i]);
// 3° Desbloquear o cadeado, transformando em camada
var layer = doc.activeLayer;
layer.blendMode = BlendMode.NORMAL;
layer.opacity = 100;
// 4° Aparar imagem
doc.trim(TrimType.TOPLEFT, true, true, true, true);
// 5° Determinando o maior lado da imagem
var longestSide = Math.max(layer.width, layer.height);
var scaleFactor = (1200 / longestSide)*100
// 6° Alterar tela de pintura 1200x1200
doc.resizeCanvas(1200, 1200, AnchorPosition.MIDDLECENTER);
// 7° CRTL+T (Transformação livre alterando a imagem para o novo tamanho calculado
layer.resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
})();
;
// 8° Save as imagem
var saveFile = new File(destinationFolder + "/" + files[i].name);
doc.saveAs(saveFile, SaveOptions.PNG);
// 9° Fechar a imagem
doc.close(SaveOptions.DONOTSAVECHANGES);
}
Explore related tutorials & articles
Copy link to clipboard
Copied
It's always good to use .value and to explicitly set pixels as the unit of measure when resizing. I'll update this post later to better explain what I mean.
EDIT:
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// YOUR CODE
app.preferences.rulerUnits = origRulerUnits;
Also, consider the following when performing math:
alert(activeDocument.width);
alert(activeDocument.width.value);
alert(activeDocument.activeLayer.width);
You need to remove "px" using .value and unlike canvas, there is no width or height of a layer, unless you do something like:
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var layerBounds = activeDocument.activeLayer.bounds;
var layerLeft = layerBounds[0].value;
var layerTop = layerBounds[1].value;
var layerRight = layerBounds[2].value;
var layerBottom = layerBounds[3].value;
var layerWidth = layerBounds[2].value - layerBounds[0].value;
var layerHeight = layerBounds[3].value - layerBounds[1].value;
var layerXCenter = layerLeft + layerWidth / 2;
var layerYCenter = layerTop + layerHeight / 2;
app.preferences.rulerUnits = origRulerUnits;
// Bounds:
// + - - - - - - - [1] - - - - - - - +
// | |
// | |
// [0] [2]
// | |
// | |
// + - - - - - - - [3] - - - - - - - +
Also take a look at the following:
Copy link to clipboard
Copied
You are using variables
newWidth
newHeight
in
layer.resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
but I don't see their definition in the code 🤷
Copy link to clipboard
Copied
Follow the right code even is not working
// 1° Determinar pasta fonte das imagens
var sourceFolder = Folder.selectDialog("Select the source folder");
// 2° Determinar pasta no qual receberá novas imagens
var destinationFolder = Folder.selectDialog("Select the destination folder");
// Get all files in the source folder
var files = sourceFolder.getFiles();
// Loop over all files
for (var i = 0; i < files.length; i++) {
// Open the file
var doc = app.open(files[i]);
// 3° Desbloquear o cadeado, transformando em camada
var layer = doc.activeLayer;
layer.blendMode = BlendMode.NORMAL;
layer.opacity = 100;
// 4° Aparar imagem
doc.trim(TrimType.TOPLEFT, true, true, true, true);
// 5° Determinando o maior lado da imagem
var longestSide = Math.max(layer.width, layer.height);
var scaleFactor = (1200 / longestSide)*100
// 6° Alterar tela de pintura 1200x1200
doc.resizeCanvas(1200, 1200, AnchorPosition.MIDDLECENTER);
// 7° CRTL+T (Transformação livre alterando a imagem para o novo tamanho calculado
layer.resize(scaleFactor, scaleFactor, AnchorPosition.MIDDLECENTER);
})();
;
// 8° Save as imagem
var saveFile = new File(destinationFolder + "/" + files[i].name);
doc.saveAs(saveFile, SaveOptions.PNG);
// 9° Fechar a imagem
doc.close(SaveOptions.DONOTSAVECHANGES);
}
Copy link to clipboard
Copied
...
var longestSide = Math.max(layer.width, layer.height);
...
By Murilo337970028e6d
An ArtLayer object does not have a "width" or "height" property. But there is a property "bounds". Read the documentation carefully and do not use fictitious properties and methods.
Copy link to clipboard
Copied
Did you get ChatGPT or another AI coding platform to write the script?

