1

How can I add a border within an image, like in the example below but preserving constant distance from the edges of that image? Currently I draw a rectangle shape with no fill, I can center it horizontally and vertically, but I don't know how to preserve the same distance on each side. Another similar question would be how can I draw an inner border preserving image aspect ratio? enter image description here

1

I'm sure there are other ways to achive this, but here's what I can think of:

  • select all via CTRL+A
  • right click anywhere and choose 'Transform Selection'
  • in the toolbar, where you have percentages, right click and choose pixels in both fields (see red dots below)
  • do the math and edit those 2 numbers, so if your image is 1000x1000 pixels and you need a 50px border on all sides, type in '900px' in both fields
  • create a new layer if you need the border as a separate layer
  • use Edit > Stroke with a white color and tick the 'Inside' option

enter image description here

enter image description here

| improve this answer | |
1

This can be easily done with a script. Save the code below as a yourname.jsx to %Photoshop Folder%/Presets/Scripts, restart Photoshop and you'll find it in File > Scripts > yourname menu and it will add a border in one click.

There're some settings you can change: stroke width, color, distance...

I'm not sure I understand what you mean by inner border preserving the aspect ratio though.

Result: enter image description here

var stroke, settings;

settings = {
  side: 20, // border distance in pixels
  strokeOnNewLayer: true, // create a border on a new layer?
  strokeWidth: 4, // width of the stroke
  strokeColor: 'ffffff', // color in HEX format (ffffff is white)
};

stroke = (function()
{

  var doc;

  var controller = function()
  {
    try
    {
      checker();
    }
    catch (e)
    {
      alert(e + '\nLine: ' + e.line);
      return false;
    }

    doc = activeDocument;

    if (settings.strokeOnNewLayer) doc.artLayers.add();

    rectSelection();

    strokeWithColor();

    doc.selection.deselect();

  };

  var rectSelection = function()
  {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putProperty(cTID('Chnl'), cTID('fsel'));
    desc.putReference(cTID('null'), ref);
    var descCoords = new ActionDescriptor();
    descCoords.putUnitDouble(cTID('Top '), cTID('#Pxl'), settings.side);
    descCoords.putUnitDouble(cTID('Left'), cTID('#Pxl'), settings.side);
    descCoords.putUnitDouble(cTID('Btom'), cTID('#Pxl'), doc.height.as("px") - settings.side);
    descCoords.putUnitDouble(cTID('Rght'), cTID('#Pxl'), doc.width.as("px") - settings.side);
    desc.putObject(cTID('T   '), cTID('Rctn'), descCoords);
    executeAction(cTID('setd'), desc, DialogModes.NO);
  };

  var strokeWithColor = function()
  {
    var strokeColor = new SolidColor();
    strokeColor.rgb.hexValue = settings.strokeColor;

    var desc = new ActionDescriptor();
    desc.putInteger(cTID('Wdth'), settings.strokeWidth);
    desc.putEnumerated(cTID('Lctn'), cTID('StrL'), cTID('Insd'));
    desc.putUnitDouble(cTID('Opct'), cTID('#Prc'), 100.000000);
    desc.putEnumerated(cTID('Md  '), cTID('BlnM'), cTID('Nrml'));
    var descData = new ActionDescriptor();
    descData.putDouble(cTID('Rd  '), strokeColor.rgb.red);
    descData.putDouble(cTID('Grn '), strokeColor.rgb.green);
    descData.putDouble(cTID('Bl  '), strokeColor.rgb.blue);
    desc.putObject(cTID('Clr '), cTID('RGBC'), descData);
    executeAction(cTID('Strk'), desc, DialogModes.NO);

  }

  var checker = function()
  {
    if (documents.length == 0) throw 'no documents found, aborting';
  };

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

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

  return controller;
})();

app.activeDocument.suspendHistory("create stroke", "stroke()");
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.