PHP生成的png背景问题

时间:2014-06-18 10:32:00

标签: php image printing png generator

我的PHP知识非常有限...所以我搜索,找到并编辑了一个脚本来在png上写文字,但透明度不存在:

<?php
$string = "username";
$string2 = "example.com";
$image = imageCreateFromPng("http://i.imgur.com/Y6hWkkW.png");
$cor = imagecolorallocate($image, 0, 0, 0);
imagestring($image,5,126,22,urldecode($string),$cor);
header('Content-type: image/png');
imagepng($image,NULL);

$stype = "png";

if ($stype = "png")
{
    // integer representation of the color black (rgb: 0,0,0)
    $background = imagecolorallocate($image, 0, 0, 0);
    // removing the black from the placeholder
    imagecolortransparent($image, $background);

    // turning off alpha blending (to ensure alpha channel information 
    // is preserved, rather than removed (blending with the rest of the 
    // image in the form of black))
    imagealphablending($image, false);

    // turning on alpha channel information saving (to ensure the full range 
    // of transparency is preserved)
    imagesavealpha($image, true);
}
?>
  
    

Live script here with the faulty result     理想的形象     enter image description here

  

我一直在网上搜索,我无法找到解决方案 如果可以,请花点时间帮助我

非常感谢

1 个答案:

答案 0 :(得分:2)

您需要'启用'透明度。

$string = "username";
$string2 = "example.com";
$image = imageCreateFromPng("http://i.imgur.com/Y6hWkkW.png");
$cor = imagecolorallocate($image, 0, 0, 0);
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image, false);
imagesavealpha($image, true);
imagestring($image,5,126,22,urldecode($string),$cor);
header('Content-type: image/png');
imagepng($image,NULL);