Skip to main content
Inspiring
November 10, 2011
Pregunta

How do i transform a layer with maintain aspect ratio through scripting in Photoshop?

  • November 10, 2011
  • 1 respuesta
  • 2755 visualizaciones

Hi,

     How do i transform a layer with maintain aspect ratio through scripting in Photoshop? I am expecting your reply.

thanks,

Rajiv.s

Este tema ha sido cerrado para respuestas.

1 respuesta

JJMack
Community Expert
Community Expert
November 10, 2011

Transform the width and height by the same percentage less then or greater then 100%. ie resize([horizontal][, vertical][, anchor]). I think Photoshop may use the users interpolation preference while resizing the layer(not sure).  Code I have used where I want to use a particular interpolation method looks like this:

var userResampleMethod = app.preferences.interpolation;

app.preferences.interpolation = ResampleMethod.BILINEAR;          // resample interpolation biliner

activeDocument.activeLayer.resize(percentageChange, percentageChange, AnchorPosition.MIDDLECENTER);

app.preferences.interpolation = userResampleMethod;

JJMack
Inspiring
November 10, 2011

Hi,

     We have two layers. one is a product image. another one is a template. Template is width : 2000 px X height : 3000 px.

the image is 6456 px width and 5601 px height. we want to match only the template layer height to Document height. we don't want to match the width.

so while transform we should click the icon of Maintain Aspect Ratio.

thanks,

Rajiv.s 

JJMack
Community Expert
Community Expert
November 10, 2011

I don't think your talking apples to apples.  First I do not know of any Maintain Aspect Ratio icon in Photoshop. The only icon I think associated with transform is the anchor point icon in the option bar for transform. I think you may referring to the constrain check box in the image size dialog which you can check when resample is checked. Image size effect all layers in the document and the documents canvas size. The document canvas size will be changed to the values in the images size dialog and all layers will be transformed by percentage the canvas was changes while maintaining the layers position over the canvas and the layers I think may be cropped so only pixels over the canvas remain.

I do not know if you know that layers can be any size and have aspect ratios different then the documents canvas.

In your case it sounds like your describing a template that has a canvas size that is 2000px wide be 3000px high a 2:3 portrait aspect ratio.  Your image file has a landscape aspect. If you place that image into your template place would by default transform the image so the image would fit within the 2000px by 3000px canvas size there would be a white border top and bottom.  You could transform that smart object layer to it actual pixels size activeDocument.activeLayer.resize(100,100, AnchorPosition.MIDDLECENTER); then calculate the size you want to transform its height to. By retrieving the canvas size and the layers boundaries. Divide the canvas height pixel size by the layers pixel height size should give you the percentage you need to use. Make sure you set the ruler units to pixels so your working with pixel values for the canvas and layer size values. The resulting layer will be larger then canvas size keeping the anchor point centered will result in the canvas size masking off both sides.  In effect cropping you landscape to a portrait.  Note cropping a image from one orientation to the other changes the composition drastically.

If your trying to make a composit like collage you may want to look at my Photoshop Collage Toolkit it will fit images to fill a 2000px by 3000px area http://www.mouseprints.net/old/dpr/PhotoCollageToolkit.html

You could also do centered 2:3 crop that is resized to a 3000px

JJMack