在Photoshop中使用脚本替换层中的图像

2022-09-04 00:00:00 javascript photoshop photoshop-script
我提供了一个JSON对象作为脚本的输入,它基本上更改了.psd文件的文本层的内容,并为我保存了一个新的.jpg。现在我想要添加一个功能,可以更改图像层中的图像。我应该做些什么才能实现该功能?

JSON对象将具有指向图像的链接。JSON输入将如下所示:

{
    "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" 
}

这是我当前的代码。请让我知道需要添加什么。

#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);
}

供您参考,LOGO是我要在其中替换为新图像的层(来自JSON输入中提供的链接)

谢谢你的帮助。非常感谢!

我附加了该文件,以便您可以查看我要编辑的内容。

我想用新徽标图像替换蓝色徽标框。


解决方案

好,第三次幸运:

您的PSD文件:

蓝色正方形大小相同的新(粉色)徽标

// 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
" + 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 );
}

相关文章