PHP - ImageCopyMerge分层图像

时间:2016-02-20 17:18:55

标签: php imagemagick gd image-manipulation

在我的问题中有人能指出我正确的方向吗? :)

我尝试将两个图像合并为一个PNG作为目标,中间是透明形状,JPG作为源,我想要在PNG图像的背面,并通过透明形状看到。

这样的事情:

来源图片:

source image

目标图片:

destination image

期望的结果:

desired result

这是我到目前为止所尝试的但是没有工作:

$dest = Imagecreatefrompng('img/dest_bg.png');
$src  = Imagecreatefromjpeg('img/src.jpg');

Imagealphablending($dest, true);
Imagealphablending($src, true);
Imagesavealpha($dest, true);

Imagecopymerge($dest, $src, 200, 0, 0, 0, 400, 415, 100);

imagepng($dest......

我曾尝试过反之亦然,但透明的星形形状是白色或棕色的pixeled颜色。

1 个答案:

答案 0 :(得分:1)

我已经成功使用此功能 imagecopymerge_alpha

这是代码

   function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
    // creating a cut resource
    $cut = imagecreatetruecolor($src_w, $src_h);

    // copying relevant section from background to the cut resource
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

    // copying relevant section from watermark to the cut resource
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

    // insert cut resource to destination image
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}

            $dest = Imagecreatefrompng('img/final_bg.png');
            $src2 = Imagecreatefromjpeg('uploads/picture.jpg');
            $src = Imagecreatefrompng('img/pink_bg.png');

            Imagealphablending($dest, true);
            Imagealphablending($src, true);
            Imagesavealpha($dest, true);


            Imagecopymerge($src, $src2, 310, 120, 0, 0, 200, 200, 100); // i have positioned a small picture on a color filled background in the middle because the final background, witch is a picture with a transparent shape in the middle 
            imagepng($src, 'uploads/temp.png');
            Imagedestroy($src2);
            Imagedestroy($src);
            $temp =  Imagecreatefrompng('uploads/temp.png');

            Imagealphablending($temp, false);
            Imagecopymerge_alpha($temp, $dest, 0, 0, 0, 0, 800, 420, 100); //merged the color filled background with the picture on it with the final background here..


            imagejpeg($temp, 'uploads/final_result.jpg');
            Imagedestroy($dest);
            Imagedestroy($temp);