问题
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