Copy link to clipboard
Copied
Hello,
I am trying to create an action to help me edit images for my store. There are several steps to editing each picture, and therefore I decided making an action would be easier to go through everything.
I have 2 layers, one is the item (without a background) and one is the background. I would like the item to always have the same width, that way it looks a bit more uniform on the website. However, since they are different objects, before making them the same size, they always have different sizes.
My issue is, when I do the action, and use transform to make the item layer a specific size, it is translated into a percentage. Therefore, the images are resized by a specific percentage, instead of resizing to a specific size, like I wished. The action says "Transform current layer" and then the Width is for example 76,9%. But as you can imagine, different sizes, resized to 76,9% mean different final pixel sizes.
Does anyone know how to make an action that resizes the image always to a specific pixel size and not percentage?
The JS to resize a layer to a specified pixel width would be something like this where the active layer is resized to 500 pixels:
#target photoshop
var pxWidth = 500;
resizePixelWidth(app.activeDocument.activeLayer, pxWidth)
/**
* Resize layer to specified pixel width
* @Param the layer to resize
* @Param the resized pixel width
* @Return void
*
*/
function resizePixelWidth(l, pw){
preferences.rulerUnits = Units.PIXELS;
var b = l.bounds;
var w = b[2]-b[0];
var s = (pw/w)
...
Copy link to clipboard
Copied
You will most likely need to write a script you can use in your action. Actions can not use logic and actions record layer transforms as a relative transform to current layer size you record the action on . Also when you transform a layer you should only use a constrained transform like the one in your action where the width and height are resized by the same percentage. its a simple calculation. The percentage would be YourWidthPixel/LayerBowndsWidth*100.
However what you need is a Template for the Web page you want. Then you can populate your images into the template. A simple two image collage template. Free Photoshop Photo Collage and Toolkit
Copy link to clipboard
Copied
The JS to resize a layer to a specified pixel width would be something like this where the active layer is resized to 500 pixels:
#target photoshop
var pxWidth = 500;
resizePixelWidth(app.activeDocument.activeLayer, pxWidth)
/**
* Resize layer to specified pixel width
* @Param the layer to resize
* @Param the resized pixel width
* @Return void
*
*/
function resizePixelWidth(l, pw){
preferences.rulerUnits = Units.PIXELS;
var b = l.bounds;
var w = b[2]-b[0];
var s = (pw/w)*100;
l.resize(s, s, AnchorPosition.MIDDLECENTER)
return
}
Copy link to clipboard
Copied
Thanks for sharing Rob!
I would advise adding the following line of code before the function (either before or after the first var):
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = origRulerUnits;
This way the user's original ruler unit values are returned after the layer resize.
Copy link to clipboard
Copied
Thank you so much! The code was really helpful!
Copy link to clipboard
Copied
The alternative that does record pixels is to convert the layer to a smart object, then edit the smart object and resize (using fit image, image size etc). Then close and save the smart object to update the layer.
Copy link to clipboard
Copied
Stephen that will resize every layer in the document not a particular image layer
Copy link to clipboard
Copied
Stephen that will resize every layer in the document not a particular image layer
Of course it won't do that!
Only the layer is being converted to a SO, not the entire document.
Layer panel options menu, convert to smart object. Only the active/targeted/selected layer is turned into a SO.
I did test this before posting, you obviously didn't.
Copy link to clipboard
Copied
Fit image resized the document not a single layer here the code in fit image. I missed the part where you wrote edit the object that is why I made the post. My error... Yes the smart would be resized. If not a good thing to do if you are populating a mockp template. Replacement need to be resized to fit the object.
function ResizeTheImage(width, height) {
var oldPref = app.preferences.rulerUnits;
var docWidth;
var docHeight;
var docRatio;
var newWidth;
var newHeight;
var resolution = app.activeDocument.resolution;
var limit = sizeInfo.limit;
app.preferences.rulerUnits = Units.PIXELS; // save old preferences
// original width, height
docWidth = (1.0 * app.activeDocument.width * resolution) / 72.0; // decimal inches assuming 72 dpi (used in docRatio)
docHeight = (1.0 * app.activeDocument.height * resolution) / 72.0; // ditto
if (docWidth < 1.0 || docHeight < 1.0)
return true; // error
if (width < 1 || height < 1)
return true; // error
if ( limit && ( app.activeDocument.width.as('px') <= width && app.activeDocument.height.as('px') <= height ) ){
app.preferences.rulerUnits = oldPref; // restore old prefs
isCancelled = false; // if get here, definitely executed
return false; // no error
}
docRatio = docWidth / docHeight; // decimal ratio of original width/height
newWidth = width;
newHeight = ((1.0 * width) / docRatio); // decimal calc
if (newHeight > height) {
newWidth = docRatio * height; // decimal calc
newHeight = height;
}
// resize the image using a good conversion method while keeping the pixel resolution
// and the aspect ratio the same
app.activeDocument.resizeImage(newWidth, newHeight, resolution, ResampleMethod.BICUBIC);
app.preferences.rulerUnits = oldPref; // restore old prefs
isCancelled = false; // if get here, definitely executed
return false; // no error
}
Copy link to clipboard
Copied
I am dealing with the same problem, where and what you have to do with the smart object to work?
Copy link to clipboard
Copied
I am dealing with the same problem, where and what you have to do with the smart object to work?
By @Bende28934224bznm
1) Convert the layer to a smart object layer (presuming a pixel layer)
2) Double click to edit the smart object layer, a new window will open with the .psb file
3) Use image size or the automate/fit image command to work in pixels, this will be recorded as a pixel unit adjustment and not % based in an action
4) Save and close the .psb file
5) The layer will be updated in the original file. You can convert the smart object back to a layer, or rasterize the layer etc.
Copy link to clipboard
Copied
Hi all,
Trying the above but getting stuck. It all goes well if I do it manually, but with a recorded action, it gives an error at the saving step. How can I solve it?
Thank you in advance
Mattia