Copy link to clipboard
Copied
I am currently selecting an image and resizing the canvas around it which works fine, however, I need to create a white fill layer and be placed at the end so I can use transparent images. I currently have some js which create a new fill layer but it sits on top and just gives me a blank white png file. If someone could show me how to place this below the image layer it would be create
var NoImages = File.openDialog("Select your logo file", false);
if (NoImages !== null) {
var doc = open(NoImages, OpenDocumentType.PNG.JPEG); // Open PNG file
if (doc == null) {
throw "Something is wrong with the file.";
}
var startState = doc.activeHistoryState; // save for undo
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS; // use pixels
// Folder selection dialog
var destFolder = Folder.selectDialog( "Choose an output folder");
if (destFolder == null) {
// User canceled, just exit
throw "";
}
// Save icons in PNG using Save for Web.
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;
doc.info = null; // delete metadata
var no_images = [
{"name": "no-image-1-1", "width":1170, "height":1170},
{"name": "no-image-2-3", "width":779, "height":1170},
{"name": "no-image-3-2", "width":1170, "height":779},
{"name": "no-image-3-4", "width":879, "height":1170},
{"name": "no-image-4-3", "width":1170, "height":879},
{"name": "no-image-7-2", "width":1170, "height":334},
{"name": "no-image-9-3", "width":1170, "height":391},
{"name": "no-image-11-5", "width":1170, "height":532},
{"name": "no-image-16-9", "width":1170, "height":658}
];
var no_image;
for (i = 0; i < no_images.length; i++) {
no_image = no_images;
doc.resizeCanvas(no_image.width, no_image.height, // width, height
null, ResampleMethod.BICUBICSHARPER);
var layerRef = app.activeDocument.artLayers.add();
layerRef.name = "fill";
var myColor = new SolidColor();
myColor.rgb.red = 255;
myColor.rgb.green = 255;
myColor.rgb.blue = 255;
activeDocument.selection.fill( myColor);
var destFileName = no_image.name + ".png";
doc.exportDocument(new File(destFolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);
doc.activeHistoryState = startState; // undo resize
}
alert("No Images created!");
}
Copy link to clipboard
Copied
I'm trying to understand what you are trying to do. It looks like you are coding resize canvas like your are doing a resize image. the statement is all wrong its just width height and anchor point. If you were to resize your logo not the canvas but the image it would get more and more distorted by these unconstrained image resize. Why would you want to put your logo the same size on nine different size white canvases which I think is what you are trying to do. Where would you like your logo to be on each canvas? Normally you place your logo on image. sized for the image's size and resolution. That is very easy to do. I have posted as script to do the several times.
Here it is again with a slight change you can position you logo in the lower left or right corner now.
#target photoshop;
app.bringToFront();
var logoFile = "~/Desktop/JJMack.png"; // Watermark file should be large for resize down works better than up
var LogoSize = 10; // percent of document height to resize Watermark to
var LogoMargin = 1; // percent of Document height the Watermark should have as a margin
var BottomLetf = false; // false = Bottom Right true Bottom Left
placeWatermark(logoFile, LogoSize, LogoMargin); // Place Watermark into the bottom right of document
function placeWatermark(Image,Size,Margin){
if(!documents.length) return; // if no document return
try{
var doc = app.activeDocument; // set Doc object to active document
app.displayDialogs = DialogModes.NO; // Dialog off
var strtRulerUnits = app.preferences.rulerUnits; // Save Users ruler units
var strtTypeUnits = app.preferences.typeUnits; // Save Users Type units
app.preferences.rulerUnits = Units.PIXELS; // work with pixels
app.preferences.typeUnits = TypeUnits.PIXELS; // work with pixels
var fileObj = new File(Image); // the passed file
if(!fileObj.exists){ // If file does not exits tell user
alert(fileObj.name + " does not exist!");
return;
}
placeFile(fileObj); // Place in file the Watermark png file
activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer
var SB = activeDocument.activeLayer.bounds; // get layers bounds
var layerHeight = SB[3] - SB[1]; // get layers height
var resizePercent = (100/layerHeight)*(Size/100*doc.height.value); // Percent to resize by
activeDocument.activeLayer.resize(resizePercent ,resizePercent,AnchorPosition.MIDDLECENTER); // Resize width and height by percentage
SB = activeDocument.activeLayer.bounds; // get resized layers bounds
activeDocument.activeLayer.translate(-SB[0].value,-SB[1].value); // Move resized layer to top left canvas corner
var LayerWidth = (SB[2].value - SB[0].value);
var LayerHeight = (SB[3].value - SB[1].value);
marginSize = Margin/100*doc.height.value; // Margin size
// move resized watermark into the document lower right corner with some margin or lower left
if ( BottomLetf) {activeDocument.activeLayer.translate(marginSize,( doc.height.value -marginSize - LayerHeight));}
else {activeDocument.activeLayer.translate((doc.width.value -marginSize - LayerWidth),( doc.height.value -marginSize - LayerHeight));}
}
catch(e) { alert(e + ': on line ' + e.line); } // inform user of error
finally{
app.preferences.rulerUnits = strtRulerUnits; // Restore user ruler units
app.preferences.typeUnits = strtTypeUnits; // Restore user type units
};
};
function placeFile(placeFile) {
var desc21 = new ActionDescriptor();
desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
var desc22 = new ActionDescriptor();
desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
};
Copy link to clipboard
Copied
Try adding this to your script:
createFill ();
var fillLayer = doc.activeLayer;
fillLayer.move(doc.layers[doc.layers.length-1], ElementPlacement.PLACEAFTER)
function createFill(){
var idMk = charIDToTypeID( "Mk " );
var desc4 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
ref1.putClass( idcontentLayer );
desc4.putReference( idnull, ref1 );
var idUsng = charIDToTypeID( "Usng" );
var desc5 = new ActionDescriptor();
var idType = charIDToTypeID( "Type" );
var desc6 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc7 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc7.putDouble( idRd, 255.000000 );
var idGrn = charIDToTypeID( "Grn " );
desc7.putDouble( idGrn, 255.000000 );
var idBl = charIDToTypeID( "Bl " );
desc7.putDouble( idBl, 255.000000 );
var idRGBC = charIDToTypeID( "RGBC" );
desc6.putObject( idClr, idRGBC, desc7 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc5.putObject( idType, idsolidColorLayer, desc6 );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
desc4.putObject( idUsng, idcontentLayer, desc5 );
executeAction( idMk, desc4, DialogModes.NO );
}