Copy link to clipboard
Copied
i need to resize layer 1 to the same size as the background, automatically.
I tried with actions, but it doesn't work, how to make a script?
Here is a simple script to distort the active layer to match the canvas. The layer to resize needs to be pre-selected/targeted in the layers panel before running the script. When recording the action you can use an absolute/explicit selection of "Layer 1" or you can use a relative keyboard shortcut to select the uppermost layer opt/alt . (dot/period character):
//community.adobe.com/t5/photoshop/scale-layer-to-current-canvas-size-photoshop/td-p/5217315
// Michael L Hale
function main() {
...
Copy link to clipboard
Copied
Hi, you need to record an action then it works for you first need to learn record to it... regards
Copy link to clipboard
Copied
thanks for responding, but as i said in the post. action does not work for this job
Copy link to clipboard
Copied
You will need a script. Try searching for one on the forums.
What should the script do to match the layer size to the background/canvas size?
Should the script distort/stretch both the width and height to match?
Or should the script only size the width of the layer to match the canvas, potentially making the height proportionally larger or smaller than the width?
Or should the script only size the height of the layer to match the canvas, potentially making the width proportionally larger or smaller than the height?
Are all the images canvas that will be processed portrait orientation, or will some be landscape? Should the script intelligently scale to "best fit" for the orientation of portrait or landscape canvas?
Copy link to clipboard
Copied
Here is a simple script to distort the active layer to match the canvas. The layer to resize needs to be pre-selected/targeted in the layers panel before running the script. When recording the action you can use an absolute/explicit selection of "Layer 1" or you can use a relative keyboard shortcut to select the uppermost layer opt/alt . (dot/period character):
//community.adobe.com/t5/photoshop/scale-layer-to-current-canvas-size-photoshop/td-p/5217315
// Michael L Hale
function main() {
var doc = app.activeDocument;
var layer = doc.activeLayer;
layer.translate(new UnitValue(0 - layer.bounds[0].as('px'), 'px'), new UnitValue(0 - layer.bounds[1].as('px'), 'px'));
layer.resize((doc.width.value / (layer.bounds[2] - layer.bounds[0])) * 100, (doc.height.value / (layer.bounds[3] - layer.bounds[1])) * 100, AnchorPosition.TOPLEFT);
}
app.activeDocument.suspendHistory('Resize Layer to Canvas', 'main()');
EDIT: This variation will select the layer named "Layer 1":
function main() {
var doc = app.activeDocument;
doc.activeLayer = doc.layers.getByName("Layer 1");
var layer = doc.activeLayer;
layer.translate(new UnitValue(0 - layer.bounds[0].as('px'), 'px'), new UnitValue(0 - layer.bounds[1].as('px'), 'px'));
layer.resize((doc.width.value / (layer.bounds[2] - layer.bounds[0])) * 100, (doc.height.value / (layer.bounds[3] - layer.bounds[1])) * 100, AnchorPosition.TOPLEFT);
}
app.activeDocument.suspendHistory('Resize Layer to Canvas', 'main()');
You can record the script into an action as a step.
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
EDIT: And here is another script that achieves the same result:
Copy link to clipboard
Copied
Thank you very much!!!
This script worked perfectly.
Copy link to clipboard
Copied
Glad to be of help and thank you for updating the topic with a correct answer to help others in the future (not everybody has the courtesy to come back, say thank you and or give feedback, press like or mark correct).
Copy link to clipboard
Copied
This code is great for layering images
But I want to use this code on a paragraph text or a text layer, but I find that the text has changed the font .. Is it possible to match the font size with the dimensions of the design
These images show a text layer and its dimensions outside the design
When executing the code, the dimensions of the text are adapted to the dimensions of the design, but the font size changes
I want a code that matches the font size with the dimensions of the text layer and the dimensions of the design
Copy link to clipboard
Copied
If it wasn't obvious, the hack to that script was never intended to be used on text layers. I'll give it a look, working with text frames and content in Photoshop can be challenging.
Copy link to clipboard
Copied
For a scripted solution, I believe that this would require advanced scripting knowledge of Action Manager code, which is beyond my current experience or abilities.
Easy to do manually though, with the text tool active, double click the text layer icon or click on the layer on the canvas to insert the i-beam insertion point into the text layer so that the content is active for editing, then manually resize the frame/bounding box to the document edges and confirm the change of the bounding box without actually changing any of the actual text content.
Copy link to clipboard
Copied
The code you made works really well
But it only requires scaling the font size to fit the document size
Copy link to clipboard
Copied
The text content is affected by the text bouding box modification.
In order to adjust the bounding box separately from the content, one must enter the modal text editing mode and then manually adjust the bounding box while in the modal edit (which prohibits scripting while in the modal edit).
As I said in my first post:
"If it wasn't obvious, the hack to that script was never intended to be used on text layers."
"working with text frames and content in Photoshop can be challenging"
"For a scripted solution, I believe that this would require advanced scripting knowledge of Action Manager code, which is beyond my current experience or abilities."
Copy link to clipboard
Copied
Thank you for your interest
Copy link to clipboard
Copied
Try searching for the forum with keywords similar to "script resize text box" or variations of. One such hit: