具有透明度/Alpha 背景的 PHP GD 文本
好吧,所以我在将文本放在部分透明的图像上时遇到问题。我希望文本是实心的,但我希望图像背景的一部分是透明的,而文本结束的部分是实心的,我有,问题是文本继承了其中之一的透明背景之前的层。这是代码和输出示例,以及在该输出下我希望它的样子。图像位于浅灰色背景上,因此图像周围深灰色之间的浅色边框是透明的,但没有其他内容特别是文本。看起来透明的不是文本本身,而是文本块的背景。正如你所看到的,这并不是很理想。请帮助,这是我完成项目所需的唯一问题。 :)
还无法发布图像,因此这里是示例输出和所需结果的图像的链接 ( 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):
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈哈,我想我没有认真思考。解决方案是在将文本放置到图像上之前重新打开 imagealphablending。
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.