使用GD Library叠加图像使它们成为灰度。为什么?

时间:2013-11-22 15:35:54

标签: php gd

我有两张PNG图像,我想创建一个新的PNG图像。第一张图片将被强加。这些代码完美运行并产生预期的图像,但所有颜色都丢失了,输出是灰度图像。

这是我的代码

Phone.png文件为“http://i44.tinypic.com/2vwilb8.png” and background.png是“http://i43.tinypic.com/2194eu9.png

    $backgroundImage = 'background.png';
    $userImage = "http://graph.facebook.com/4/picture?type=large";
    $resultImage ='phone.png';


    $background = imagecreatefrompng($backgroundImage);
    $userResultImage = imagecreatefromjpeg($userImage);
    $result = imagecreatefrompng($resultImage);


    list($backgroundWidth, $backgroundHeight) = getimagesize($backgroundImage);
    list($userImageWidth, $userImageHeight) =  getimagesize($userImage);
    list($resultRightItemWidth, $resultRightItemHeight) = getimagesize($resultImage);

    $newWidth = 800;
    $newHeight = 800;

    $new = imagecreate($newWidth, $newHeight);
    imagealphablending($new, false);
    imagesavealpha($new, true);

    imagecopy($new, $background, 0, 0, 0, 0, $backgroundWidth, $backgroundHeight);
    imagecopy($new, $userResultImage, 40, 300, 0, 0, $userImageWidth, $userImageHeight);
    header('Content-type: image/png');

    imagepng($new,'asdas.png');

    imagecopy($new, $result, 490, 170, 0, 0, $resultRightItemWidth, $resultRightItemHeight);
    header('Content-type: image/png');
    imagepng($new, $userID.'.png');

1 个答案:

答案 0 :(得分:3)

您应该使用imagecreatetruecolor()代替imagecreate()...