7
好吧,我的文本覆盖在部分透明的图像上有问题。我希望文字是坚实的,但我希望图像的背景的一部分是透明的,文字结束的部分要坚实,我有,问题是文字是继承透明背景之一的之前的图层。这里是代码,以及输出的例子,并且在那个输出下我想要它的样子。图像铺设在浅灰色的背景上,因此在较暗的灰色之间图像周围的光线边缘是透明的,但没有其他特别应该是文字。它似乎不是自己的文本,而是透明的文本块的背景。正如你所看到的,这不是很理想。请帮助,这是我留下来完成我的项目的唯一问题。 :)带有透明度/ 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);
?>
其实我有类似的问题,在添加文本之前变成alphablending就像魅力一样!谢谢! – ikhsan 2013-11-06 11:46:46
是的,我不得不在关闭imagecopyresampled()调整大小之前关闭它,然后在将文本与imagettftext()放在一起以保持背景透明并避免字符周围出现灰色菱形 – vladkras 2014-01-23 14:11:31