0

我已经尽我所能尝试解决这个问题。我花了一个多小时研究和尝试代码,但没有任何帮助。

此代码执行以下操作。

  • 拍摄两张全白图像并重新着色(同时保持透明度
  • 将两个图像合并在一起
  • 输出图像(但背景为黑色!!)

有人可以帮助识别和修补导致黑色背景的部分吗?有关脚本示例,请参见以下 URL。

  • http://labs.bluefiremedia.net/metro-machine/gd/download-png.php?size=128&padding=29&icon=icons/Application/Add-New.png&bgShape=CircleBG.png&bgColorR=255&bgColorG=0&bgColorB=0&iconColorR=255&iconColorG= 255&图标颜色​​B=255

    $final_image = imagecreatetruecolor($dimensions, $dimensions);
    imagesavealpha($final_image, true);
    
    if($bgShape != '') {
    list($originalWidth, $originalHeight) = getimagesize('../images/' . $bgShape);
    
    $background = imagecreatefrompng('../images/' . $bgShape);
    
    imagefilter($background, IMG_FILTER_BRIGHTNESS, -255);
    imagefilter($background, IMG_FILTER_COLORIZE, $bgColorR, $bgColorG, $bgColorB);
    
    $backgroundImage = imagecreatetruecolor( $dimensions, $dimensions );
    imagealphablending($backgroundImage , false);
    imagesavealpha($backgroundImage , true);
    
    imagecopyresampled($backgroundImage,
        $background,
        0, 0,
        0, 0,
        $dimensions, $dimensions,
        $originalWidth, $originalHeight
    );
    
    imagecopy($final_image, $backgroundImage, 0, 0, 0, 0, $dimensions, $dimensions);
    
    ///
    
    $icon = imagecreatefrompng("../" . $icon);
    
    imagefilter($icon, IMG_FILTER_BRIGHTNESS, -255);
    imagefilter($icon, IMG_FILTER_COLORIZE, $iconColorR, $iconColorG, $iconColorB);
    
    $iconImage = imagecreatetruecolor( $dimensions, $dimensions );
    imagealphablending($iconImage , false);
    imagesavealpha($iconImage , true);
    
    imagecopyresampled($iconImage,
        $icon,
        0, 0,
        0, 0,
        $dimensions, $dimensions,
        $originalWidth, $originalHeight
    );
    
    imagecopy($final_image, $iconImage, 0, 0, 0, 0, $dimensions, $dimensions);
    
    ///
    
    imagealphablending($final_image, true);
    imagesavealpha($final_image, true);
    
    imagepng($final_image, NULL, 0, PNG_NO_FILTER);
    
    header("Content-type: image/png");
    imagedestroy($backgroundImage);
    
4

1 回答 1

2

设置imagealphablending为 false,用透明颜色填充图像,设置imagealphablending为 true,然后进行复制。

$final_image = imagecreatetruecolor($dimensions, $dimensions);
imagealphablending($final_image, false);
$transparency = imagecolorallocatealpha($final_image,  0, 0, 0, 127);
imagefilledrectangle($final_image, 0, 0, $dimensions, $dimensions, $transparency);
imagesavealpha($final_image, true);
imagealphablending($final_image, true);

// rest of the code
于 2013-05-12T23:31:19.613 回答