[FIXED] Ersetzen Sie das Bild in einer Ebene in Photoshop mit Scripting

Ausgabe

Ich stelle ein JSON-Objekt als Eingabe für mein Skript bereit, das im Wesentlichen den Inhalt der Textebene der Datei ändert und ein neues für mich .psdspeichert . .jpgJetzt möchte ich eine Funktion hinzufügen, die das Bild in der Bildebene ändert. Was soll ich tun, um diese Funktionalität zu erreichen?.

Das JSON-Objekt enthält den Link zum Bild. Die JSON-Eingabe würde wie folgt aussehen:

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

Das ist mein aktueller Code. Bitte lassen Sie mich wissen, was hinzugefügt werden muss.

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

Geben Sie hier die Bildbeschreibung ein

Als Referenz ist das Logo die Ebene, in der ich ein neues Bild ersetzen möchte (aus dem Link in der JSON-Eingabe).

Danke für die Hilfe. Sehr geschätzt!

Ich hänge die Datei an, damit Sie sehen können, was ich bearbeiten möchte.

Geben Sie hier die Bildbeschreibung ein

Ich möchte das blaue Logo-Feld durch ein neues Logo-Bild ersetzen.

Lösung

Ok, drittes Mal Glück:

Deine psd-Datei:

Quelle PSD

neues (rosa) Logo, das die gleiche Größe wie das blaue Quadrat hat

rosa Logo

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


Beantwortet von –
Ghoul Fool


Antwort geprüft von –
Timothy Miller (FixError Admin)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like