Imagick PHP Extension -- Montage help?
Asked Answered
O

2

5

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!!

Opal answered 13/5, 2010 at 21:25 Comment(0)
E
3

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++);
}
Envelope answered 12/10, 2010 at 21:50 Comment(2)
Too bad you have to do this by hand, but at least your advice saved me a serious headache. I needed to montage images in a 2x2 layout but with little adjustement your code snippet is perfectly fit.Avie
Please, see the answer below. It uses the montageImage and works.Quadrat
B
4

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.

Breathing answered 21/6, 2013 at 4:41 Comment(0)
E
3

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++);
}
Envelope answered 12/10, 2010 at 21:50 Comment(2)
Too bad you have to do this by hand, but at least your advice saved me a serious headache. I needed to montage images in a 2x2 layout but with little adjustement your code snippet is perfectly fit.Avie
Please, see the answer below. It uses the montageImage and works.Quadrat

© 2022 - 2024 — McMap. All rights reserved.