Copy link to clipboard
Copied
Hello Everybody
I'm looking for a script that would resize a layer to fit a canvas of 34cm wide by 40cm high, but that would also take into account the aspect ratio of the layer so it doesn't warp the image.
I'm really sorry I'm absolutely useless at script writing and admit I have no skill in this area, any help would be amazing. Thank you
Copy link to clipboard
Copied
There is a scripting forum and if you search it you will find code there that will answer your question. You can also read Adobe Fit Image Plug-in script to see how whole documents can be resized and maintain aspect ratio. Its simple math.
You can also fit an imag into and area without distorting it by resizing it to fill the area then masking off any excess . A virtual center crop to the new aspect ratio. For example the script will resize the current layer to fit within four guides you first set.
http://www.mouseprints.net/old/dpr/FitImageToGuides.jsx
Copy link to clipboard
Copied
Thanks, I'll give that a go.
Copy link to clipboard
Copied
With that script if you set guides to the canvas four any current layer above the background canvas would be resized to fill the canvas and excess would be outside the canvas area. Layers other then the background layer can be any size. It would be best if any layer you want to resize is converted to a smart object layer first before you uset that script.
Copy link to clipboard
Copied
I would agree. about putting the layer in a smart object first. You can use scriptlistener to get the code for that. The above script does size the layer to cover the entire file's canvas. If you don't want to loose anything outside the canvas, change the word "max" in line 8 to "min."
Copy link to clipboard
Copied
If the current Photoshop target layer is a like a raster or text layer, a layer that can be resized and positioned. Chuck's script with max changed to min will do what you want.
Like an actions may fail if the current document state is not what the actions is depending on. Other add-ons like scripts and plug-ins may also fail if some condition is not satisfied. All code is not bullet proof.
Copy link to clipboard
Copied
Yes, I didn't put any error checking into this script. It's just the bare bones.
Copy link to clipboard
Copied
Yes I know but new users do not realize that Scripts and actions can also have side effects. I only wanted to point out we don't live in a perfect world.
Like if their Photoshop preferences units were set to inches they will be set to pixels after using that script. We know this and don't care. For Its extra work to save everything, check everything and restore things. We don't care for my own use. I can't even type so the extra code is time consuming for me.
New users do not understand this they just scratch the heads and think I could have swan I set units to inches. I just like point this out from time to time.
Copy link to clipboard
Copied
That is very true!
Copy link to clipboard
Copied
As JJ Mack stated it's just math: figuring out your file size in pixels and you layer size in pixels, then sizing the layer.
#target photoshop
var oldPref = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;
var doc = activeDocument;
var iLayer = doc.activeLayer;
doc.activeLayer = iLayer;
var scale = Math.max(doc.width/(iLayer.bounds[2]-iLayer.bounds[0]),doc.height/(iLayer.bounds[3]-iLayer.bounds[1]));
iLayer.resize (scale*100,scale*100);
iLayer.translate(doc.width/2-(iLayer.bounds[0]+iLayer.bounds[2])/2,doc.height/2-(iLayer.bounds[1]+iLayer.bounds[3])/2);
Moving to Photoshop Scripting forum