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

发布于 2024-08-18 17:33:25 字数 1376 浏览 4 评论 0原文

好吧,所以我在将文本放在部分透明的图像上时遇到问题。我希望文本是实心的,但我希望图像背景的一部分是透明的,而文本结束的部分是实心的,我有,问题是文本继承了其中之一的透明背景之前的层。这是代码和输出示例,以及在该输出下我希望它的样子。图像位于浅灰色背景上,因此图像周围深灰色之间的浅色边框是透明的,但没有其他内容特别是文本。看起来透明的不是文本本身,而是文本块的背景。正如你所看到的,这并不是很理想。请帮助,这是我完成项目所需的唯一问题。 :)

还无法发布图像,因此这里是示例输出和所需结果的图像的链接 ( orig):

在此处输入图像描述

<?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);
imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);

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

?>

alright so im having a problem with getting my text layed over a partly transparent image. i want the text to be solid, but i want part of the background of the image to be transparent, and the part the text is over to be solid, which i have, the problem is the text is inheriting the transparent background of one of the previous layers. here is the code, and an example of the output, and under that output what i want it to look like. the image is laying on a light grey background so the light border around the image in between the darker grey is transparent but nothing else should be especially the text. it seems to be not the text its self but the background of the text blocks that is transparent. which as you can see isn't very desirable. please help, this is the only problem i have left to complete my project. :)

can't post images yet, so heres a link to the image of example output and desired outcome (orig):

enter image description here

<?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);
imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);

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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-08-25 17:33:25

哈哈,我想我没有认真思考。解决方案是在将文本放置到图像上之前重新打开 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);

?>

hah i guess i didn't think hard enough on it. the solution was to turn imagealphablending back on before laying the text onto the image.

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文