png图像复制到另一个图像php

时间:2014-06-27 14:49:30

标签: php image png

我正在尝试将png图像添加到另一个图像上。我唯一的问题是png图像在它周围创建一个透明背景而不是另一个图像的背景。

这是图像:

enter image description here

预期结果:

enter image description here

这是代码:

<?php
    $img_name = "image_" . date("U") . ".png";
    $whoareyou_src = imagecreatefrompng('who-are-you.png');

    create_image($img_name, $whoareyou_src);
    print "<img src=". $img_name .">";

    function  create_image($img_name, $whoareyou_src) {
        $im = @imagecreate(800, 610) or die("Cannot Initialize new GD image stream");
        $background_color = imagecolorallocate($im, 0, 128, 128);  // teal

        // imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
        $success = imagecopy($im, $whoareyou_src, 0, 0, 0, 0, imagesx($whoareyou_src), imagesy($whoareyou_src));
        echo "Image Copy: " . $success . "<br>"; // testing

        imagepng($im, $img_name);

        imagedestroy($im);
        imagedestroy($whoareyou_src);
    }

?>

我想将背景设置为透明可能会有所帮助:

imagecolortransparent($whoareyou_src, imagecolorallocate($whoareyou_src, 0, 0, 0));

但这并没有改变任何事情。

更新

尝试使用imagecopyresampledalpha设置,但结果仍然相同:

<?php
    $img_name = "image_" . date("U") . ".png";
    $whoareyou_src = imagecreatefrompng('who-are-you.png');

    create_image($img_name, $whoareyou_src);
    print "<img src=". $img_name .">";

    function  create_image($img_name, $whoareyou_src) {
        $im = @imagecreate(800, 610) or die("Cannot Initialize new GD image stream");
        imagealphablending($im, false);
        imagesavealpha($im,true);

        $background_color = imagecolorallocate($im, 0, 128, 128);  // teal

        $success = imagecopyresampled($im, $whoareyou_src, 0, 0, 0, 0, imagesx($whoareyou_src), imagesy($whoareyou_src), imagesx($whoareyou_src), imagesy($whoareyou_src));
        echo "Image Copy: " . $success . "<br>";

        imagepng($im, $img_name);

        imagedestroy($im);
        imagedestroy($whoareyou_src);
    }

?>

2 个答案:

答案 0 :(得分:2)

尝试这样的事情......     

create_image($img_name, $whoareyou_src);
print "<img src=". $img_name .">";

function  create_image($img_name, $whoareyou_src) {
    $im = @imagecreatetruecolor(800, 610) or die("Cannot Initialize new GD image stream");
    imagealphablending($im, false);
    imagesavealpha($im,true);

    $background_color = imagecolorallocate($im, 0, 128, 128);  // teal

    // imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
    $success = imagecopyresampled($im, $whoareyou_src, 0, 0, 0, 0, imagesx($whoareyou_src), imagesy($whoareyou_src));
    echo "Image Copy: " . $success . "<br>"; // testing

    imagepng($im, $img_name);

    imagedestroy($im);
    imagedestroy($whoareyou_src);
}

&GT;

答案 1 :(得分:1)

这应该可以解决问题:

<?php
    $img_name = "image_" . date("U") . ".png";
    $whoareyou_src = imagecreatefrompng('who-are-you.png');

    create_image($img_name, $whoareyou_src);
    print "<img src=". $img_name .">";

    function  create_image($img_name, $whoareyou_src) {
        $im = @imagecreatetruecolor(800, 610) or die("Cannot Initialize new GD image stream");
        $background_color = imagecolorallocate($im, 0, 128, 128);  // teal
        imagefilledrectangle($im, 0, 0, 800, 610, $background_color);

        // imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
        $success = imagecopy($im, $whoareyou_src, 0, 0, 0, 0, imagesx($whoareyou_src), imagesy($whoareyou_src));
        echo "Image Copy: " . $success . "<br>"; // testing

        imagepng($im, $img_name);

        imagedestroy($im);
        imagedestroy($whoareyou_src);
    }

?>