Agamnentzar / ag-psd

Javascript library for reading and writing PSD files
Other
489 stars 66 forks source link

Text content updated in PSD layer object but image remains unchanged #174

Open AsiF-Py opened 4 months ago

AsiF-Py commented 4 months ago

I encountered an issue while using the ag-psd library in Node.js to programmatically update text content in a PSD file. Despite successfully updating the text content in the PSD layer object, the image remains unchanged after saving the modified PSD file.

Here's the relevant code snippet:

import * as fs from 'fs';
import 'ag-psd/initialize-canvas.js';
import { readPsd, writePsdBuffer } from 'ag-psd';

const buffer = fs.readFileSync('UK DL FRONT.psd');
const psd = readPsd(buffer);

const kumonLayer = psd.children[4].children.find(layer => layer.name === 'KUMON');

if (kumonLayer) {
    kumonLayer.text.text = 'MD. Asif';
    console.log('Updated text content:', kumonLayer.text.text);
    console.log('Updated layer:', kumonLayer.name);

    const modifiedBuffer = writePsdBuffer(psd);
    fs.writeFileSync('modified_UK_DL_FRONT.psd', modifiedBuffer);
    console.log('Text for KUMON layer updated successfully.');
} else {
    console.log('Layer with name "KUMON" not found.');
}

Expected behavior:

I expect that when I update the text content of the PSD layer object and save the modified PSD file, the image in the PSD file should also reflect the changes made to the text content.

Actual behavior:

After running the above code, the text content in the PSD layer object (kumonLayer.text.text) is updated successfully, but when I open the modified PSD file, the image remains unchanged. It seems like the changes made to the layer object are not reflected in the saved PSD file.

Agamnentzar commented 4 months ago

The library will not redraw the text layer for you, you need to provide the new bitmap yourself in layer.canvas, see more here: https://github.com/Agamnentzar/ag-psd?tab=readme-ov-file#updating-text-layers