【问题标题】:Background is turning out black背景变成黑色
【发布时间】:2013-05-12 22:53:39
【问题描述】:

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

此代码执行以下操作。

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

有人可以帮助识别和修补导致黑色背景的部分吗?有关脚本示例,请参见以下 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&iconColorB=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);
    

【问题讨论】:

    标签: php background merge gd transparent


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2016-11-25
      • 2013-07-31
      • 1970-01-01
      相关资源
      最近更新 更多