Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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_Transparent_Imagettftext - Fatal编程技术网

带有透明/Alpha背景的PHP GD文本

带有透明/Alpha背景的PHP GD文本,php,gd,transparent,imagettftext,Php,Gd,Transparent,Imagettftext,好吧,所以我有一个问题,让我的文字叠加在一个部分透明的图像。我希望文本是实心的,但我希望图像的部分背景是透明的,文本的部分是实心的,我有,问题是文本继承了前面一层的透明背景。下面是代码和一个输出示例,在该输出下面是我希望它的样子。图像位于浅灰色背景上,因此在深灰色背景之间的图像周围的光边界是透明的,但其他任何东西都不应该是文本。它似乎不是文本本身,而是文本块的背景是透明的。正如你所看到的,这不是很理想。请帮忙,这是我完成项目所剩下的唯一问题。:) 还无法发布图像,因此这里有一个指向示例输出图像和

好吧,所以我有一个问题,让我的文字叠加在一个部分透明的图像。我希望文本是实心的,但我希望图像的部分背景是透明的,文本的部分是实心的,我有,问题是文本继承了前面一层的透明背景。下面是代码和一个输出示例,在该输出下面是我希望它的样子。图像位于浅灰色背景上,因此在深灰色背景之间的图像周围的光边界是透明的,但其他任何东西都不应该是文本。它似乎不是文本本身,而是文本块的背景是透明的。正如你所看到的,这不是很理想。请帮忙,这是我完成项目所剩下的唯一问题。:)

还无法发布图像,因此这里有一个指向示例输出图像和所需结果()的链接:


哈,我想我在这件事上想得不够仔细。解决方案是在将文本放置到图像上之前重新打开imagealphablending

<?php

$img = imagecreatetruecolor(200, 50);

$imageX = imagesx($img);
$imageY = imagesy($img);

imagealphablending($img, false);
imagesavealpha($img, true);

$transparent = imagecolorallocatealpha($img, 255,255,255, 127);
$white = imagecolorallocate($img, 255,255,255);
$grey = imagecolorallocate($img, 127,127,127);
imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);
imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent);

$font = "./arialbd.ttf";
$fontSize = 12;
$text = "THIS IS A TEST";

$textDim = imagettfbbox($fontSize, 0, $font, $text);
$textX = $textDim[2] - $textDim[0];
$textY = $textDim[7] - $textDim[1];

$text_posX = ($imageX / 2) - ($textX / 2);
$text_posY = ($imageY / 2) - ($textY / 2);

imagefilledrectangle($img, 10, 10, $imageX-10, $imageY-10, $grey);
imagealphablending($img, true);
imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);

header("Content-Type: image/png");
imagepng($img);

?>


事实上,我也有类似的问题,在添加文本之前打开alphablending效果很好!谢谢是的,我必须在使用
imagecopyresampled()
调整大小之前关闭它,在使用
imagettftext()
放置文本之前重新打开它,以保持背景透明并避免字符周围出现灰色菱形
<?php

$img = imagecreatetruecolor(200, 50);

$imageX = imagesx($img);
$imageY = imagesy($img);

imagealphablending($img, false);
imagesavealpha($img, true);

$transparent = imagecolorallocatealpha($img, 255,255,255, 127);
$white = imagecolorallocate($img, 255,255,255);
$grey = imagecolorallocate($img, 127,127,127);
imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);
imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent);

$font = "./arialbd.ttf";
$fontSize = 12;
$text = "THIS IS A TEST";

$textDim = imagettfbbox($fontSize, 0, $font, $text);
$textX = $textDim[2] - $textDim[0];
$textY = $textDim[7] - $textDim[1];

$text_posX = ($imageX / 2) - ($textX / 2);
$text_posY = ($imageY / 2) - ($textY / 2);

imagefilledrectangle($img, 10, 10, $imageX-10, $imageY-10, $grey);
imagealphablending($img, true);
imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);

header("Content-Type: image/png");
imagepng($img);

?>