Drop shadow in Photoshop based on image size

I’m dealing with many thousands of psd files containing product images. Most items
have been separated from their original background and placed on white. They have a very slight drop shadow to elevate them a bit from the BG.
Now I’m overhauling them all (squared images originally but 4:3 needed) and want to give them all the exact same shadow (mainly through recorded actions). I have recorded the shadow in an action (spread of X px, distance of X px etc).

The problem now becomes that image dimensions range from around 1000 up to even 5000 px. And if I apply the same shadow it looks quite different in a smaller image compared with a larger one.

What I’m looking for is a way to create shadow not with PIXEL distance, spread etc but with PERCENT distance etc. This way I’m hoping to achieve the same APPARENT shadow in all images.
I have worked with PS for many years and searched online but never came across anything like that.

Is there a way to achieve this? Any help would be appreciated.

Answer

Here’s how I’d approach this with scripting. First I define reference shadow and reference size, then if image width isn’t the same as reference size, I linearly transform shadow values based on ratio of current image width and reference width.

Result (first image is reference, 1000×1000, second is 2000×2000, third is 660×600, fourth is 200×200):

enter image description here

Because Photoshop doesn’t support decimals in the shadow effect, the results between won’t be exactly the same, but close enough.

So you can add this script as one of the action steps:

function main()
{

    var myShadow = {
            color:
            {
                r: 0,
                g: 0,
                b: 0,
            },
            opacity: 80,
            angle: 120,
            distance: 10,
            spread: 0,
            size: 20,
        },
        referenceSize = 1000,
        doc = activeDocument,
        w = doc.width.as("px"),
        h = doc.height.as("px");

    if (w != referenceSize)
    {
        var ratio = w/referenceSize;
        myShadow.distance = linear(ratio,0,1,0,myShadow.distance);
        myShadow.spread = linear(ratio,0,1,0,myShadow.spread);
        myShadow.size = linear(ratio,0,1,0,myShadow.size);
    }

    dropShadow(myShadow);

    /////////////////////////////////////////////////////////////////////////////////////

    function dropShadow(shadow)
    {
        var desc10 = new ActionDescriptor();
        var ref1 = new ActionReference();
        ref1.putProperty(cTID('Prpr'), cTID('Lefx'));
        ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
        desc10.putReference(cTID('null'), ref1);
        var desc11 = new ActionDescriptor();
        desc11.putUnitDouble(cTID('Scl '), cTID('#Prc'), 100);
        var desc12 = new ActionDescriptor();
        desc12.putBoolean(cTID('enab'), true);
        desc12.putBoolean(sTID('present'), true);
        desc12.putBoolean(sTID('showInDialog'), true);
        desc12.putEnumerated(cTID('Md  '), cTID('BlnM'), cTID('Nrml'));
        var desc13 = new ActionDescriptor();
        desc13.putDouble(cTID('Rd  '), shadow.color.r);
        desc13.putDouble(cTID('Grn '), shadow.color.g);
        desc13.putDouble(cTID('Bl  '), shadow.color.b);
        desc12.putObject(cTID('Clr '), cTID('RGBC'), desc13);
        desc12.putUnitDouble(cTID('Opct'), cTID('#Prc'), shadow.opacity);
        desc12.putBoolean(cTID('uglg'), false);
        desc12.putUnitDouble(cTID('lagl'), cTID('#Ang'), shadow.angle);
        desc12.putUnitDouble(cTID('Dstn'), cTID('#Pxl'), shadow.distance);
        desc12.putUnitDouble(cTID('Ckmt'), cTID('#Pxl'), shadow.spread);
        desc12.putUnitDouble(cTID('blur'), cTID('#Pxl'), shadow.size);
        desc12.putUnitDouble(cTID('Nose'), cTID('#Prc'), 0.000000);
        desc12.putBoolean(cTID('AntA'), false);
        var desc14 = new ActionDescriptor();
        desc14.putString(cTID('Nm  '), "Linear");
        desc12.putObject(cTID('TrnS'), cTID('ShpC'), desc14);
        desc12.putBoolean(sTID('layerConceals'), true);
        desc11.putObject(cTID('DrSh'), cTID('DrSh'), desc12);
        desc10.putObject(cTID('T   '), cTID('Lefx'), desc11);
        executeAction(cTID('setd'), desc10, DialogModes.NO);
    };

    function linear(X, A, B, C, D, _cut)
    {
        var _cut = _cut !== undefined ? _cut : false;
        var Y = (X - A) / (B - A) * (D - C) + C
        if (_cut)
        {
            Y > D && Y = D;
            Y < C && Y = C;
        };
        return Y
    };

    function cTID(s)
    {
        return app.charIDToTypeID(s);
    };

    function sTID(s)
    {
        return app.stringIDToTypeID(s);
    };

}
app.activeDocument.suspendHistory("addShadow", "main()");

Attribution
Source : Link , Question Author : Engle , Answer Author : Sergey Kritskiy

Leave a Comment