简体   繁体   中英

Background is turning out black

I have done everything I could possibly try to fix this. I've spent over an hour researching and trying code, but nothing has helped.

This code does the following.

  • Take two completely-white images and re-color them ( while keeping transparency )
  • Merge the two images together
  • Outputs the images (but with a black background!!)

Can someone help identify and patch the part that's causing the black background? See the following URL for an example of the script.

  • 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); 

Set imagealphablending to false, fill your image with a transparent color, set imagealphablending to true, then do your copying stuff.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM