dazzafact
dazzafact

Reputation: 2860

PHP: Imagick: Merge Transparent Image

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

enter image description here

Upvotes: 2

Views: 3461

Answers (1)

emcconville
emcconville

Reputation: 24419

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

Upvotes: 6

Related Questions