php - 如何用 PHP 在图片上写不透明的文本?

标签 php image gd

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

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 channel 分配给 $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.

关于php - 如何用 PHP 在图片上写不透明的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65199177/

相关文章:

iOS 应用提交错误 : ERROR ITMS-90032: "Invalid Image Path - No image found at the path referenced under key ' CFBundleIcons': 'AppIcon29x29' "

php - 正则表达式忽略第二个前瞻

php - 使用 PDO fetch 查询 Mysql 返回 false

javascript - 确定 Canvas 插值的步骤数

php - 在 gd 和 php 中换行和居中文本

PHP GD 图像未在 Chrome 中显示

PHP GD 调整大小导致伪影

php - Ajax + 后退和前进按钮

php - 比较两个数组并从 PHP 中的数组中删除数组

java - 如何实现图像无限循环?