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

How Do I Write A Script Resizing A Layer To Canvas & Keeping The Aspect Ratio

Guest
Oct 02, 2015 Oct 02, 2015

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

TOPICS
Actions and scripting
2.0K
Translate
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
Adobe
Community Expert ,
Oct 02, 2015 Oct 02, 2015

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.

https://forums.adobe.com/search.jspa?place=%2Fplaces%2F1383833&sort=updatedDesc&q=resize+layer+aspec...



Photoshop Scripting




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

JJMack
Translate
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
Guest
Oct 02, 2015 Oct 02, 2015

Thanks, I'll give that a go.

Translate
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 02, 2015 Oct 02, 2015

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.

JJMack
Translate
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 02, 2015 Oct 02, 2015

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."

Translate
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 02, 2015 Oct 02, 2015

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.

JJMack
Translate
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 02, 2015 Oct 02, 2015

Yes, I didn't put any error checking into this script. It's just the bare bones.

Translate
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 02, 2015 Oct 02, 2015

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.

JJMack
Translate
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 02, 2015 Oct 02, 2015
LATEST

That is very true!

Translate
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 02, 2015 Oct 02, 2015

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

Translate
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