Skip to main content
showshow
Inspiring
October 28, 2016
Answered

fit canvas to specific ratio

  • October 28, 2016
  • 2 replies
  • 1398 views

Hi Scripters!

I am lokking to find a way to fit a canvas to a specific ratio with taking account of the height.

For example i have many images with diferent size, and i want all of them with the same ratio (L:2/H:3)

Thank you !

This topic has been closed for replies.
Correct answer showshow

#target photoshop

main ();

function main ()
{
  if (app.documents.length < 1)
  {
  alert ("No document open to resize.");
  return;
  }

  // These can be changed to create images with different aspect ratios.
  var arHeight = 4;
  var arWidth = 5;

  // Apply the resize to Photoshop's active (selected) document.
  var doc = app.activeDocument;

  // Get the image size in pixels.
  var pixelWidth = new UnitValue (doc.width, doc.width.type);
  var pixelHeight = new UnitValue (doc.height, doc.height.type);
  pixelWidth.convert ('px');
  pixelHeight.convert ('px');

  // Determine the target aspect ratio and the current aspect ratio of the image.
  var targetAr = arWidth / arHeight;
  var sourceAr = pixelWidth / pixelHeight;

  // Start by setting the current dimensions.
  var resizedWidth = pixelWidth;
  var resizedHeight = pixelHeight;

  // The source image aspect ratio determines which dimension, if any, needs to be changed.
  if (sourceAr < targetAr)
  resizedWidth = (arWidth * pixelHeight) / arHeight;
  else
  resizedHeight = (arHeight * pixelWidth) / arWidth;

  // Apply the change to the image.
  doc.resizeCanvas (resizedWidth, resizedHeight, AnchorPosition.MIDDLECENTER);
}

2 replies

showshow
showshowAuthorCorrect answer
Inspiring
October 31, 2016

#target photoshop

main ();

function main ()
{
  if (app.documents.length < 1)
  {
  alert ("No document open to resize.");
  return;
  }

  // These can be changed to create images with different aspect ratios.
  var arHeight = 4;
  var arWidth = 5;

  // Apply the resize to Photoshop's active (selected) document.
  var doc = app.activeDocument;

  // Get the image size in pixels.
  var pixelWidth = new UnitValue (doc.width, doc.width.type);
  var pixelHeight = new UnitValue (doc.height, doc.height.type);
  pixelWidth.convert ('px');
  pixelHeight.convert ('px');

  // Determine the target aspect ratio and the current aspect ratio of the image.
  var targetAr = arWidth / arHeight;
  var sourceAr = pixelWidth / pixelHeight;

  // Start by setting the current dimensions.
  var resizedWidth = pixelWidth;
  var resizedHeight = pixelHeight;

  // The source image aspect ratio determines which dimension, if any, needs to be changed.
  if (sourceAr < targetAr)
  resizedWidth = (arWidth * pixelHeight) / arHeight;
  else
  resizedHeight = (arHeight * pixelWidth) / arWidth;

  // Apply the change to the image.
  doc.resizeCanvas (resizedWidth, resizedHeight, AnchorPosition.MIDDLECENTER);
}

JJMack
Community Expert
Community Expert
October 28, 2016

If you want to change a document aspect ratio you need to add canvas or Crop the document. Doing that wild not make all you images the same size. They will all just have the  same  portrait aspect ration.  Any that you added canvas to will have some transparent or color border(s) on one or two sides depending on how you added canvas.

You can resize image to fit with is some canvas size. They will retain their current aspect but will be resized to fit with in the area specified.  Use menu File>Automate>Fit Image. Set the width and height in pixel in the plug-in dialog..  Being a plug-in you can record that in an action and batch the action. The Plug-in will record the settings into the action step. No dialog will be displayed when the action is used.  Do not save over the original file.

JJMack