MrE
MrE

Reputation: 1154

Overlay PNG on JPG using Imagick

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!

Upvotes: 8

Views: 11787

Answers (2)

Phil
Phil

Reputation: 2293

I was trying to overlay a png with transparency over the top of another png. I used this line from the PHP docs.

$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);

but I was getting the same problem. The overlay came through as black only. Changing it to this seemed to fix the colours.

$src1->compositeImage($src2, Imagick::COMPOSITE_DEFAULT, 0, 0);

Upvotes: 2

MrE
MrE

Reputation: 1154

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!

Upvotes: 11

Related Questions