Reputation: 81
I am providing a JSON Object as an input to my script which basically changes the content of the text layer of the .psd
file and saves a new .jpg
for me. Now I want to add a feature which changes the image in the image layer. What shall I do to achieve that functionality?.
The JSON Object will have the link to the image. The JSON Input would look like:
{
"appName": "abc",
"developerName": "XYZ Inc.",
"storeName": "Random Store",
"ctaText": "CTA Message",
"imageLink": "https://is4-ssl.mzstatic.com/image/thumb/Purple114/v4/9a/ba/d6/9abad636-8d6c-a9df-3910-607fe11f5ed6/source/100x100bb.jpg"
}
This is my current code. Please let me know what needs to be added.
#include json2.js
var input = loadJSON('test.json');
var doc = app.activeDocument;
//Changing App Name in a Text Layer
var layer9 = doc.layerSets.getByName('Layer 9');
var appNameText = layer9.layers[1];
appNameText.textItem.contents = input.appName;
//Saving the template in JPEG Format
saveJpeg(input.appName);
//Changing Developer Name in a Text Layer
var developerNameText = layer9.layers[0];
developerNameText.textItem.contents = input.developerName;
//Changing Store Name in a Text Layer
var layer3 = doc.layerSets.getByName('Layer 3');
var storeNameText = layer3.layers[0];
storeNameText.textItem.contents = input.storeName;
//Changing CTA Text in a Text Layer
var layer4 = doc.layerSets.getByName('Layer 4');
var ctaText = layer4.layers[0];
ctaText.textItem.contents = input.ctaText;
//Load JSON
function loadJSON(relPath){
var script = new File($.fileName);
var jsonFile = new File(script.path + '/' + relPath);
jsonFile.open('r');
var str = jsonFile.read();
jsonFile.close();
return JSON.parse(str);
}
//Save JPEG
function saveJpeg(name){
var file = new File(app.activeDocument.path + '/' + name + '.jpg');
var opts = new JPEGSaveOptions();
opts.quality = 10; //High quality JPEG save
doc.saveAs(file, opts, true);
}
For your reference, logo is the layer in which I want to replace with new image (coming from the link provided in the JSON input)
Thanks for the help. Much appreciated!
I am attaching the file so that you can get a look of what I want to edit.
I want to replace the blue logo box with a new logo image.
Upvotes: 1
Views: 676
Reputation: 6947
Ok, third time lucky:
Your psd file:
new (pink) logo that's the same size and the blue square
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// Call it the source document
var srcDoc = app.activeDocument;
// load image
var imageToAdd = "D:\\temp\\pink.png";
place_image_here(imageToAdd, srcDoc);
translate_layer(484, 632);
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// function PLACE IMAGE HERE (source image, destination image)
// --------------------------------------------------------
function place_image_here(fromimage, toimage)
{
var fileRef = new File(fromimage);
// If it's there, open it!
if (fileRef.exists)
{
app.open(fileRef);
// Establish the newly opened doc
// is the from document
var fromDoc = app.activeDocument;
var fromDocName = app.activeDocument.name;
// Get the name of the destination image
var toImageName = toimage.name;
// Establish the from and to documents
var to = app.documents.getByName(toImageName);
var from = app.documents.getByName(fromDocName);
// Select the tempImage
app.activeDocument = from;
// Move from tempImage to the baseImage
var duplicateLayer = activeDocument.activeLayer.duplicate(to);
// Close the temp image without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
else
{
alert("Error opening\n" + fromimage);
}
}
// function TRANSLATE LAYER(dx, dy)
// --------------------------------------------------------
function translate_layer(dx,dy)
{
// =======================================================
var id2014 = charIDToTypeID( "Trnf" );
var desc416 = new ActionDescriptor();
var id2015 = charIDToTypeID( "null" );
var ref287 = new ActionReference();
var id2016 = charIDToTypeID( "Lyr " );
var id2017 = charIDToTypeID( "Ordn" );
var id2018 = charIDToTypeID( "Trgt" );
ref287.putEnumerated( id2016, id2017, id2018 );
desc416.putReference( id2015, ref287 );
var id2019 = charIDToTypeID( "FTcs" );
var id2020 = charIDToTypeID( "QCSt" );
var id2021 = charIDToTypeID( "Qcsa" );
desc416.putEnumerated( id2019, id2020, id2021 );
var id2022 = charIDToTypeID( "Ofst" );
var desc417 = new ActionDescriptor();
var id2023 = charIDToTypeID( "Hrzn" );
var id2024 = charIDToTypeID( "#Pxl" );
desc417.putUnitDouble( id2023, id2024, dx );
var id2025 = charIDToTypeID( "Vrtc" );
var id2026 = charIDToTypeID( "#Pxl" );
desc417.putUnitDouble( id2025, id2026, dy );
var id2027 = charIDToTypeID( "Ofst" );
desc416.putObject( id2022, id2027, desc417 );
executeAction( id2014, desc416, DialogModes.NO );
}
Upvotes: 1