Copy link to clipboard
Copied
Hey guys, Im new here and with photoshop so Im not even sure if my request is possible to be done but I would like to try anyway. I need a script to auto fit my photo onto a shape instead the background.
I already got a script that auto fits my photo with the backgroud by as you can see on my screenshot I need something that can make the photo auto fit on the shape of the under layer and not the whole background. So I would say a "fit in" script, I know I could just resize it but Im currently working with this so in case I got a script could save me a lot time on the long run.
And the sizes of my frames/shapes are different thats why would need something that fits the shape under the photo and not just resize to an expecif size. Thanks 🙂
(Photoshop mac/os)
Copy link to clipboard
Copied
There are different approaches you can take to make a photo the size of a particular shape such as using a Clipping Mask or Smart Objects. Based on your screenshots, it looks like you want to scale the photo up. While you can scale an image up, it's much better if you can scale it down or use a Smart Object so that the scaled image doesn't become pixelated. Here's more info about Clipping Masks and Smart Objects if you'll be working with larger images:
Copy link to clipboard
Copied
Have you tried using the Frame Tool (K)?
Copy link to clipboard
Copied
// scale smart objects to width of next layer below if it has a layer mask;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theSmartObjects = collectSmartObjects2022();
for (var m = 0; m < theSmartObjects.length; m++) {
if (hasLayerMask (theSmartObjects[m][1]-1) == true) {
// get bounds;
var groupBounds = getBounds(theSmartObjects[m][1]-1);
if (groupBounds != undefined) {
////////////////////////////////////
if (theSmartObjects[m][3] != groupBounds[1] + 2) {
selectLayerByID(theSmartObjects[m][2], false);
executeAction( stringIDToTypeID( "placedLayerResetTransforms" ), undefined, DialogModes.NO );
var layerBounds = getBounds(theSmartObjects[m][1]);
////////////////////////////////////
var theScale = (groupBounds[1]+2) / layerBounds[1] * 100;
var xOffset = groupBounds[3] - layerBounds[3];
var yOffset = groupBounds[4] - layerBounds[4];
////////////////////////////////////
var idtransform = stringIDToTypeID( "transform" );
var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
var idpercentUnit = stringIDToTypeID( "percentUnit" );
var idoffset = stringIDToTypeID( "offset" );
var desc5 = new ActionDescriptor();
var idfreeTransformCenterState = stringIDToTypeID( "freeTransformCenterState" );
var idquadCenterState = stringIDToTypeID( "quadCenterState" );
var idQCSAverage = stringIDToTypeID( "QCSAverage" );
desc5.putEnumerated( idfreeTransformCenterState, idquadCenterState, idQCSAverage );
var desc6 = new ActionDescriptor();
desc6.putUnitDouble( stringIDToTypeID( "horizontal" ), idpixelsUnit, xOffset );
desc6.putUnitDouble( stringIDToTypeID( "vertical" ), idpixelsUnit, yOffset );
desc5.putObject( idoffset, idoffset, desc6 );
desc5.putUnitDouble( stringIDToTypeID( "width" ), idpercentUnit, theScale );
desc5.putUnitDouble( stringIDToTypeID( "height" ), idpercentUnit, theScale );
executeAction( idtransform, desc5, DialogModes.NO );
}
}
////////////////////////////////////
}
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjects2022 () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
// determine if background layer exists to change index;
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd = 1}
else {theAdd = 0};
var theNumber = desc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theW = theBounds.getUnitDoubleValue(stringIDToTypeID("right")) - theBounds.getUnitDoubleValue(stringIDToTypeID("left"));
var theH = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom")) - theBounds.getUnitDoubleValue(stringIDToTypeID("top"));
theLayers.push([theName, theIndex-theAdd, theID, theW, theH, theseBounds])
}
};
}
catch (e) {};
};
return theLayers
};
////// has layer mask //////
function hasLayerMask (theIndex) {
//if (theIndex >= 0) {return undefined};
try {
var m_Dsc01, m_Ref01;
m_Ref01 = new ActionReference();
m_Ref01.putIndex(stringIDToTypeID("layer"), theIndex);
m_Dsc01 = executeActionGet(m_Ref01);
return m_Dsc01.hasKey(charIDToTypeID("Usrs"));
} catch (e) {return undefined}
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// bounds of active layer //////
function getBounds (theIndex) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("bounds"));
ref.putIndex(stringIDToTypeID("layer"), theIndex);
var layerDesc = executeActionGet(ref);
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theW = theBounds.getUnitDoubleValue(stringIDToTypeID("right")) - theBounds.getUnitDoubleValue(stringIDToTypeID("left"));
var theH = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom")) - theBounds.getUnitDoubleValue(stringIDToTypeID("top"));
var horCenter = theseBounds[0] + theW / 2;
var verCenter = theseBounds[1] + theH / 2;
return ([theseBounds, theW, theH, horCenter, verCenter])
} catch (e) {return undefined}
};