Imagick PHP Extension — Montage help?

泄露秘密 提交于 2019-12-05 00:50:34

问题


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


回答1:


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++);
}



回答2:


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.



来源:https://stackoverflow.com/questions/2830427/imagick-php-extension-montage-help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!