如何在 PHP 的图片上写不透明的文字?

How to write text with opacity on a picture in PHP?

我正在图片上写文字。到目前为止一切都很好,但我必须降低此文本的透明度或不透明度值。 我尝试了以下方法来获取不透明度值,但我无法得到任何结果。

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

您可以使用 imagecolorallocatealpha() 将 alpha 通道分配给 $textcolor :

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

注意:

A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.