Questions & Answers

Replace image in PSD file

fellow programmers!

I'm trying to build a tool to replace an image within a .psd file. Atm I'm playing around with a PHP script that utilizes the imagick / ImageMagic extension.

I'd be extremely grateful if somebody would help me in how to accomplish this using imagick, or point me into an alternative direction.

The problem is that the image does not get replaced. I've tried using both setImage and addImage. If I export the file within the if statement, it shows the newly added image, however the changes are not reflected in the output.

This is the code I've tried.

        $src = Storage::path('file.psd');
        $psd = new Imagick($src);

        foreach ($psd as $index => $layer) {

            // Skip the first layer
            if (! $index) {

            $imageProperties = $layer->getImageProperties();
            $label = $imageProperties['label'] ?? '';

            if (str($label)->lower()->startsWith(['+'])) {
                $layer->setImage(new Imagick(Storage::path('image.png')));


2023-01-21 23:30:10
My initial response was going to be “PSD is not supported” but after a quick search I was proven wrong! Good to know! I’m seeing people mention that the first (probably bottom-most) layer has to be a flattened layer, and possibly a background layer. Maybe try that?
Answers(0) :