具有透明度/Alpha背景的PHP GD文本 [英] PHP GD Text with Transparency/Alpha background

查看:82
本文介绍了具有透明度/Alpha背景的PHP GD文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在将我的文字放置在部分透明的图像上时遇到了问题.我希望文本是纯色的,但是我希望图像背景的一部分是透明的,而文本结束的那一部分是纯色的,这是我有的,问题是文本继承了其中一个的透明背景前几层.这是代码,以及输出示例,在该输出下,我希望它看起来像.图像位于浅灰色背景上,因此较深的灰色之间的图像周围的浅色边框是透明的,但没有其他内容,尤其是文本.似乎不是文本本身,而是透明的文本块背景.如您所见,这不是很理想.请帮助,这是我完成项目所剩下的唯一问题. :)

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. :)

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

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

?>

推荐答案

我猜我对此没有足够的思考.解决方案是在将文本放置到图像上之前重新打开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.

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

?>

这篇关于具有透明度/Alpha背景的PHP GD文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆