How Layer Flipping Works in Photoshop Scripting
Horizontal Flip:
layer.resize(-100, 100, AnchorPosition.MIDDLECENTER)Vertical Flip:
layer.resize(100, -100, AnchorPosition.MIDDLECENTER)Both (180° / Corner Flip):
layer.resize(-100, -100, AnchorPosition.MIDDLECENTER)
#target photoshop
function createMirrorBleed() {
if (!app.documents.length) return;
var doc = app.activeDocument;
var origUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS; // Use Pixels for exact boundary alignment
doc.suspendHistory("4-Side Mirror Bleed", "runMirrorDOM()");
app.preferences.rulerUnits = origUnits;
}
function runMirrorDOM() {
var doc = app.activeDocument;
// 1. Flatten to establish a single base source
if (doc.layers.length > 1) doc.flatten();
var origLayer = doc.activeLayer;
if (origLayer.isBackgroundLayer) origLayer.isBackgroundLayer = false;
// Calculate 1cm bleed equivalent in pixels based on document DPI
var bleedPx = Math.round((doc.resolution / 2.54) * 1); // 1 cm in pixels
var w = doc.width.value;
var h = doc.height.value;
// 2. Expand Canvas
doc.resizeCanvas(w + (bleedPx * 2), h + (bleedPx * 2), AnchorPosition.MIDDLECENTER);
// Helper: Create mirrored layer edge via duplication & crop/position
function makeEdge(x1, y1, x2, y2, flipH, flipV, transX, transY) {
doc.activeLayer = origLayer;
doc.selection.select([[x1, y1], [x2, y1], [x2, y2], [x1, y2]]);
// Copy selection via clipboard to bypass ActionManager
doc.selection.copy();
var newL = doc.paste();
doc.selection.deselect();
if (flipH) newL.flip(Direction.HORIZONTAL);
if (flipV) newL.flip(Direction.VERTICAL);
newL.translate(transX, transY);
return newL;
}
function makeEdge(x1, y1, x2, y2, flipH, flipV, transX, transY) {
doc.activeLayer = origLayer;
doc.selection.select([[x1, y1], [x2, y1], [x2, y2], [x1, y2]]);
// Copy selection to new layer
doc.selection.copy();
var newL = doc.paste();
doc.selection.deselect();
// Use negative scaling percentages to flip the layer
var scaleX = flipH ? -100 : 100;
var scaleY = flipV ? -100 : 100;
if (flipH || flipV) {
newL.resize(scaleX, scaleY, AnchorPosition.MIDDLECENTER);
}
// Position the mirrored piece
newL.translate(transX, transY);
return newL;
}
// Coordinates relative to resized canvas (centered original sits at offset bleedPx)
var xL = bleedPx;
var xR = bleedPx + w;
var yT = bleedPx;
var yB = bleedPx + h;
// 3. Generate Edges
var topL = makeEdge(xL, yT, xR, yT + bleedPx, false, true, 0, -bleedPx);
var botL = makeEdge(xL, yB - bleedPx, xR, yB, false, true, 0, bleedPx);
var leftL = makeEdge(xL, yT, xL + bleedPx, yB, true, false, -bleedPx, 0);
var rightL = makeEdge(xR - bleedPx, yT, xR, yB, true, false, bleedPx, 0);
// 4. Generate Corners
makeEdge(xL, yT, xL + bleedPx, yT + bleedPx, true, true, -bleedPx, -bleedPx);
makeEdge(xR - bleedPx, yT, xR, yT + bleedPx, true, true, bleedPx, -bleedPx);
makeEdge(xL, yB - bleedPx, xL + bleedPx, yB, true, true, -bleedPx, bleedPx);
makeEdge(xR - bleedPx, yB - bleedPx, xR, yB, true, true, bleedPx, bleedPx);
// 5. Re-order main artwork layer to top
origLayer.move(topL, ElementPlacement.PLACEBEFORE);
}
createMirrorBleed();
