Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php imagecreatefrompng()png的透明部分为黑色_Php_Image - Fatal编程技术网

Php imagecreatefrompng()png的透明部分为黑色

Php imagecreatefrompng()png的透明部分为黑色,php,image,Php,Image,我正在使用PHP创建海报,我想在海报上添加数字 我使用以下代码添加它们: $src1 = imagecreatefrompng("m2.png"); $widthsrc=0; $heightsrc=0; list($widthsrc, $heightsrc, $typesrc, $attrsrc) = getimagesize("m2.png"); $background = imagecolorallocate($src1, 0, 0, 0); imagecolortransparent($

我正在使用PHP创建海报,我想在海报上添加数字

我使用以下代码添加它们:

$src1 = imagecreatefrompng("m2.png");
$widthsrc=0;
$heightsrc=0;
list($widthsrc, $heightsrc, $typesrc, $attrsrc) = getimagesize("m2.png");

$background = imagecolorallocate($src1, 0, 0, 0);
imagecolortransparent($src1, $background);
imagealphablending($src1, false);
imagesavealpha($src1, true);

imagecopyresampled($my_img,$src1,$line2X1+100*$resize,$line2Y1,0,0,1000*$resize,1000*$resize,$widthsrc,$heightsrc);
问题是数字应该透明的地方是黑色的

我已经看过以下帖子:


  • 但是我还没有能够创建一个适合我的解决方案。

    那么,将注释转换为答案很容易:

    你的错误是定义了背景色。您应该使用以下选项:

    $background = imagecolorallocatealpha($src,0,0,0,127);
    
    然而,这可能是一个好主意,以安全,并避免使用“透明”的颜色,已经存在于您的图像。“传统”的透明颜色从旧的精灵为基础的游戏是洋红,因为它是非常不可能的,你会有直接的洋红在你的形象

    $background = imagecolorallocatealpha($src,255,0,255,127);
    

    那么你的问题是什么呢?
    $background
    应该是
    imagecoloralallocatealpha($src1,0,0,0127)。就我个人而言(本着旧精灵游戏的精神),我会使用
    255,0255127
    ,因为洋红是图像的“传统”透明颜色。感谢@NiettheDarkAbsol解决了这个问题。