首页 > 解决方案 > 如何在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

标签: phpimagegd

解决方案


您可以使用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);

注意 :

介于 0 和 127 之间的值。0 表示完全不透明,而 127 表示完全透明。


推荐阅读