Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 470
MrE Avatar asked Mar 08 '13 14:03

MrE


People also ask

What is the use of IMagick class in PHP?

The Imagick class utilises the exception handling model introduced in PHP5 and thus we will do that as well. Let's presume, that we have a directory in our filesystem, which contains our program and the two images we want to operate on.

Can ImageMagick be used as a command line tool?

Commonly, ImageMagick is used as a command-line tool, which makes it suitable for batch operations. There are many GUI clients which use ImageMagick underhood for performing image manipulation operations. How to perform PNG to JPG conversion using ImageMagick?

What to do if IMagick extension is not loaded?

throw new Exception('Imagick extension is not loaded.'); throw new Exception('Imagick class does not exist.'); Also a couple more words on the Imagick::COMPOSITE_DEFAULT argument.

How to compose one image onto another using setimageartifact?

Composite one image onto another at the specified offset. Any extra arguments needed for the compose algorithm should passed to setImageArtifact with 'compose:args' as the first parameter and the data as the second parameter. Composite operator. See Composite Operator Constants


1 Answers

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!

like image 165
MrE Avatar answered Oct 18 '22 21:10

MrE