【问题标题】:Image Overlay in PHP; Black Background?PHP中的图像叠加;黑色背景?
【发布时间】:2010-12-12 01:52:18
【问题描述】:

当我使用 GD 库在 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);

?>

【问题讨论】:

    标签: php image background overlay


    【解决方案1】:
    // Create an image
    
    $img = imagecreatetruecolor($imgWidth, $imgHeight);
    
    $white = imagecolorallocate($img, 255, 255, 255);
    
    // Make the background white
    
    imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);
    

    ...可以帮忙。

    【讨论】:

    • 我将帖子的脚本编辑为我的最新脚本。仍然显示为黑色。
    【解决方案2】:

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

    如果您要尝试做的不仅仅是最基本的图像处理,我建议您可以通过使用功能更强大(但不幸的是文档很少)imagick 库来让您的生活更轻松;它更快、更容易(同样,一旦您通过文档)和更强大。

    【讨论】:

    • 我已经搜索了 3 天,但似乎没有任何效果。
    • @Seth:正如 El Yobo 链接的讨论中所述,您应该在分配白色之前分配一个虚拟颜色,因为您不能使用第一个分配的颜色作为透明度。此外,在定义代码之前,您可以在代码中使用 $white