PHP: Imagick: Merge Transparent Image

折月煮酒 提交于 2020-07-03 10:38:48

问题


I want to merge a transparent PNG into another Image, but the Border of the PNG will not change to Transparent as a want.

    <?
    $s1=    new Imagick('img.jpg');
    $s2=    new Imagick('img2.jpg');
    $s2->setImageFormat ('png');
    $s2->vignetteImage(20, 20, 40, - 20); 
    $s2->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
    $s1->compositeImage($s2, Imagick::COMPOSITE_DEFAULT,120,120, Imagick::CHANNEL_ALPHA);


回答1:


You just need to set the background to transparent on the original JPEG image.

<?
$s1=    new Imagick('img.jpg');
$s2=    new Imagick('img2.jpg');
$s2->setImageFormat ('png');
$s2->setImageBackgroundColor("transparent"); // <= Here
$s2->vignetteImage(20, 20, 40, - 20); 
$s2->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$s1->compositeImage($s2, Imagick::COMPOSITE_DEFAULT,120,120, Imagick::CHANNEL_ALPHA);

If there's still issues, it might be worth ensuring the alpha channel of the PNG is enabled.

$s2->setImageMatte(TRUE);


来源:https://stackoverflow.com/questions/32553605/php-imagick-merge-transparent-image

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