Question

I've been having some trouble generating an image with the Imagick PHP extension. Everything works fine, except my following "montage" has a white background and therefore I cannot overlay it on top of something else. How can I generate a montage with a transparent background?

       $Montage = $Icons->montageImage(new imagickdraw(), "3x2+0+0", "34x34+3+3", imagick::MONTAGEMODE_UNFRAME, "0x0+0+0");
      $Canvas->compositeImage($Montage, $Montage->getImageCompose(), 5, 5);

Thanks!!

Was it helpful?

Solution

I had the same problem and discovered that the MagickWand C API, which powers imagick), doesn't support the option for montage.

I ended up montaging it manually like this:

// Add them to an array of Imagick objects, instead of using addImage().
$images = new Array();

// Make a transparent canvas.
$target = new Imagick();
$target->newImage($width, $height * count(images), 'none');

$i = 0;
foreach ($images as $image) {
    $target->compositeImage($image, imagick::COMPOSITE_COPY, 0, $height * $i++);
}

OTHER TIPS

I know this is an old question, but I found another way to do this using Imagick's montageImage function. After you create your Imagick object you must declare the background as transparent like this:

$Icons->setBackgroundColor( new ImagickPixel('transparent') );

Once that is set, you can run the object through montageImage which will create a montageImage with a transparent background:

$Montage = $Icons->montageImage(new imagickdraw(), "3x2+0+0", "34x34+3+3", imagick::MONTAGEMODE_UNFRAME, "0x0+0+0");

Then you can add the new montage image to your composite image making sure to use the predefined Imagick composite constant COMPOSITE_ATOP or your desired constant(s) like this:

$Canvas->compositeImage($Montage, imagick::COMPOSITE_ATOP, 5, 5);

Just ran across this question and decided to post another solution in case someone else wants another way to do this without a manual loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top