2010-12-12 63 views
1

当我使用GD库在php中执行和图像叠加时,我总是会得到黑色背景,但是,所有图像都正确叠加。有人可以帮忙吗?PHP中的图像叠加;黑色背景?

<?php 

    $images = array($_GET['color'], $_GET['face'], $_GET['hat']); 


    $img = imagecreatetruecolor(58, 75); 

    imagealphablending($img, true); 
    imagesavealpha($img, true); 

    imagecolorallocate($img, 255, 205, 255); 
    imagecolorallocate($img, 255, 255, 255); 
    imagecolortransparent($img, $white); 

imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white); 
    foreach($images as $fn) { 

     $cur = imagecreatefrompng($fn); 
     imagealphablending($cur, true); 
     imagesavealpha($cur, true); 


     imagecopy($img, $cur, 0, 0, 0, 0, 58, 75); 


     imagedestroy($cur); 
    } 


    header('Content-Type: image/png'); 
    imagepng($img); 

?> 

回答

0
// Create an image 

$img = imagecreatetruecolor($imgWidth, $imgHeight); 

$white = imagecolorallocate($img, 255, 255, 255); 

// Make the background white 

imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white); 

...可能会有帮助。

+0

我编辑帖子的脚本到我最新的脚本。仍然显示黑色。 – Seth 2010-12-12 14:34:30

+0

啊!得到它了!谢谢。 – Seth 2010-12-12 19:16:29

0

这是一个常见问题,答案已经在stack overflow上可用;那里的答案完美地解决了这个问题。你可能想尝试更难搜索:)

我建议你可以通过使用功能更强大(但不幸记录不佳)的imagick库来让你的生活更轻松,如果你打算尝试做更多的事情最基本的图像处理;它更快,更容易(再次,一旦你通过文档),更强大。

+0

我一直在寻找3天,似乎没有任何工作。 – Seth 2010-12-12 14:27:11

+0

@Seth:正如在El Yobo讨论中指出的那样,您应该在分配白色之前分配一个虚拟颜色,因为您无法将第一个分配的颜色用作透明度。另外,在定义代码之前,您在代码中使用'$ white'。 – nico 2010-12-12 14:38:42

+0

所以这样? (往上看) – Seth 2010-12-12 14:48:39