Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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
将alpha通道添加到PHP GD图像_Php_Gd - Fatal编程技术网

将alpha通道添加到PHP GD图像

将alpha通道添加到PHP GD图像,php,gd,Php,Gd,我想使用PHP的GD函数创建一个透明的png图像。 具体地说,我希望文本具有不同的不透明度级别(用于消除混叠)。 使用以下代码,我能够在背景的主要部分创建alpha: //Create image $image = imagecreatetruecolor($width, $height); //Set background to opaque imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0)); // crea

我想使用PHP的GD函数创建一个透明的png图像。 具体地说,我希望文本具有不同的不透明度级别(用于消除混叠)。 使用以下代码,我能够在背景的主要部分创建alpha:

//Create image
$image = imagecreatetruecolor($width, $height);
//Set background to opaque
imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0));
// create image resource.
$img = imagecreatetruecolor(250, 200);
// save alpha channel information (for transparent background).
imagealphablending($img, false);
imagesavealpha($img, true);

// create image colours.
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
$black_0     = imagecolorallocatealpha($img, 0, 0, 0, 0);
$black_25    = imagecolorallocatealpha($img, 0, 0, 0, 32);
$black_50    = imagecolorallocatealpha($img, 0, 0, 0, 64);
$black_75    = imagecolorallocatealpha($img, 0, 0, 0, 96);

// set background to transparent.
imagefill($img, 0, 0, $transparent);

// output text strings.
imagettftext($img, 20, 0, 10, 30,  $black_0,   'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60,  $black_25,  'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90,  $black_50,  'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $black_75,  'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $transparent, 'arial.ttf', '100% transparent'); // not visible!

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
虽然它确实正确地创建了一个alpha,但在该区域中,图像的不透明度级别不是0%或100%,它会变成黑色。 如何正确创建图像中这些区域的不透明度级别

具体来说,我有不同不透明度级别的文本(用于 消除混叠)

对文本使用不同的不透明度级别不会对其进行反别名。没有理由这么做,因为GD无论如何都会输出抗锯齿文本

示例1:这将创建一个具有不透明黑色背景的图像,其中白色文本具有不同的不透明级别

// create image resource.
$img = imagecreatetruecolor(250, 200);

// create image colours.
$black     = imagecolorallocate($img, 0, 0, 0);
$white_0   = imagecolorallocatealpha($img, 255, 255, 255, 0);
$white_25  = imagecolorallocatealpha($img, 255, 255, 255, 32);
$white_50  = imagecolorallocatealpha($img, 255, 255, 255, 64);
$white_75  = imagecolorallocatealpha($img, 255, 255, 255, 96);
$white_100 = imagecolorallocatealpha($img, 255, 255, 255, 127);

// set background to opaque black.
imagefill($img, 0, 0, $black);

// output text strings.
imagettftext($img, 20, 0, 10, 30,  $white_0,   'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60,  $white_25,  'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90,  $white_50,  'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $white_75,  'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $white_100, 'arial.ttf', '100% transparent'); // not visible!

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
结果1:

示例2:如果您想要的是透明背景:

//Create image
$image = imagecreatetruecolor($width, $height);
//Set background to opaque
imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0));
// create image resource.
$img = imagecreatetruecolor(250, 200);
// save alpha channel information (for transparent background).
imagealphablending($img, false);
imagesavealpha($img, true);

// create image colours.
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
$black_0     = imagecolorallocatealpha($img, 0, 0, 0, 0);
$black_25    = imagecolorallocatealpha($img, 0, 0, 0, 32);
$black_50    = imagecolorallocatealpha($img, 0, 0, 0, 64);
$black_75    = imagecolorallocatealpha($img, 0, 0, 0, 96);

// set background to transparent.
imagefill($img, 0, 0, $transparent);

// output text strings.
imagettftext($img, 20, 0, 10, 30,  $black_0,   'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60,  $black_25,  'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90,  $black_50,  'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $black_75,  'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $transparent, 'arial.ttf', '100% transparent'); // not visible!

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
结果2:


谢谢。。。这将是可行的,但是,我正在使用一个在GIMP中构建的带有特殊功能的徽标,我无法使用GD创建这些功能…好吧,在这种情况下,我真的不明白你想要什么。请用你想要达到的目标的示例图片更新你的问题。奇怪。。。它突然起作用了。谢谢你的帮助,很抱歉我的问题很难理解。