如何在PHP中实现文字透明度覆盖在图片上?

3

我正在为一张图片添加文字,目前一切都很好,但我需要降低这段文字的透明度或不透明度数值。我尝试了以下方法来改变透明度值,但没有得到任何结果。

imagecolortransparent($image, imagecolorallocate($image, 0,0,0));



header('Content-type: image/jpeg');
$size = 50;
$degrees = 0;
$rl = 200;
$xy = 120;
$font = 'arial.ttf';
$text = "Watermark Text";
$image = imagecreatefromjpeg('image.jpg');
$textcolor = imagecolorallocate($image, 230, 230, 230);
imagettftext($image, $size, $degrees ,$rl, $xy, $textcolor, $font, $text);
imagejpeg($image); 
imagedestroy($image);

实际结果: https://prnt.sc/vy8axf 期望结果: https://prnt.sc/vy8cnz

请阅读[ask]。解释一下你实际尝试了什么,而不仅仅告诉我们你尝试了“某些东西”。 - CBroe
@CBroe 你是对的 :) 我已经安排好了。 - codefile
1个回答

3
你可以使用 imagecolorallocatealpha() 函数为 $textcolor 分配 alpha 通道:
$textcolor = imagecolorallocatealpha($image, 230, 230, 230, 100);

示例:

$size = 50;
$degrees = 0;
$rl = 200;
$xy = 120;
$font = 'arial.ttf';
$text = "Watermark Text";

$image = imagecreatefromjpeg('image.jpg');
$textcolor = imagecolorallocatealpha($image, 230, 230, 230, 100);
imagettftext($image, $size, $degrees ,$rl, $xy, $textcolor, $font, $text);

header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

请注意:
值应在0和127之间。 0表示完全不透明,而127表示完全透明。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接