Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glyons/68cc1cd2f5e0779c2cab4de847999d30 to your computer and use it in GitHub Desktop.
Save glyons/68cc1cd2f5e0779c2cab4de847999d30 to your computer and use it in GitHub Desktop.
// Images into PSD Template (PSD must have a layer called 'artwork')
// The PSD and images should be all in the same directory
var fWidth = 1046;
var fHeight = 479;
var inFolder = Folder.selectDialog("Please select folder to process");
if (inFolder != null)
{
var fileList = inFolder.getFiles(/\.(jpg)$/i);
var templateList = inFolder.getFiles(/\.(psd)$/i);
for(var i = 0; i < templateList.length; i++)
{
makeframe("Template-" + i + "-", inFolder, templateList[i]);
}
}
function makeframe(exportPrefixName, exportPath, psdFile)
{
// main loop starts here
for(var i = 0; i < fileList.length; i++)
{
var appDoc = app.open(psdFile);
//pref pixels
app.preferences.rulerUnits = Units.PIXELS;
// load the frames one by one
var doc = open(fileList[i]);
var tempImage = app.activeDocument.name;
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
//2:3
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
appDoc.rotateCanvas(90);
}
else if (doc.height==doc.width)
{
//1:1
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
appDoc.rotateCanvas(90);
}
else {
//3:2
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
//select all
activeDocument.selection.selectAll()
//copy image
activeDocument.selection.copy();
//close that document without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
getMeThisLayer("artwork")
//paste
app.activeDocument.paste();
//deselect all
activeDocument.selection.deselect()
var filePath = exportPath + "/" + exportPrefixName + tempImage;
// Flatten the image
activeDocument.flatten();
// save out the image
var saveFile = new File(filePath);
var saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 1;
activeDocument.saveAs(saveFile, saveOptions, false, Extension.LOWERCASE);
// close that save
app.activeDocument.close();
}
}
function getMeThisLayer(aLayerName)
{
try
{
// try to find the layer
app.activeDocument.activeLayer = app.activeDocument.layers.getByName(aLayerName)
return
}
catch(e)
{
//Whoops can't find layer
alert("Can't find layer " + aLayerName + "\n" + e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment