5

我在使用 Imagick PHP 扩展生成图像时遇到了一些麻烦。一切正常,除了我下面的“蒙太奇”有白色背景,因此我不能将它覆盖在其他东西之上。如何生成具有透明背景的蒙太奇?

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

谢谢!!

4

2 回答 2

4

我知道这是一个老问题,但我找到了另一种使用 Imagick 的 montageImage 函数的方法。创建 Imagick 对象后,您必须将背景声明为透明,如下所示:

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

设置完成后,您可以通过 montageImage 运行该对象,这将创建一个具有透明背景的 montageImage:

$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.

于 2013-06-21T04:41:12.980 回答
3

我遇到了同样的问题,发现为 imagick 提供支持的 MagickWand C API 不支持蒙太奇选项。

我最终像这样手动蒙版:

// 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++);
}
于 2010-10-12T21:50:09.490 回答