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) {
continue;
}
$imageProperties = $layer->getImageProperties();
$label = $imageProperties['label'] ?? '';
if (str($label)->lower()->startsWith(['#'])) {
$layer->removeImage();
$layer->setImage(new Imagick(Storage::path('image.png')));
}
}
$psd->mergeImageLayers(Imagick::LAYERMETHOD_MERGE);
$psd->setImageFormat('png');
$psd->writeImage(Storage::path('output.png'));
$psd->clear();
$psd->destroy();
Here's an example file that I've tried: https://mega.nz/file/H5FhQRRZ#Ta06TySXU5FIKAtx0DN87E3tjN2QSDoa3DAx0AQQbZ0