Overlay PNG on JPG using Imagick

MrE picture MrE · Mar 8, 2013 · Viewed 9.8k times · Source

I have the last few hours tried to get a PNG logo with a transparent background on top of a JPG background. I have tried several ways and with several globals as well, but I do not seem to be able to get the result I want.

"First Attempt":

$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');

$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background

$overlay->destroy();
$image->destroy();

enter image description here

As you can see, the Jaguar logo is all black.


"Second Attempt"

$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');

$image->setImageColorspace($overlay->getImageColorspace() ); 
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background

$overlay->destroy();
$image->destroy();

enter image description here

This one the Jaguar logo looks like it should, but the background is all messed up now.


I have tried with Imagick::setImageMatte and tried to add the overlay to a white background (thought I does need to have a transparent background) and still it won't display the image properly. I have tried many other variations of the 2 above snippets but they all seem to make the PNG completely or partial black.

What am I missing or doing wrong? Can anyone give me a nudge in the right direction? Please note this needs to be done in PHP.

Thank you very much!

Answer

MrE picture MrE · Mar 28, 2013

I am a huge idiot! Turns out I forgot to convert the images from CMYK to RGB. For anyone who might encounter this in the future, learn from my incompetence!