php - 如何在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);
解决方案
您可以使用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 表示完全透明。
推荐阅读
- android - MaterialButtonToggleGroup 强制间距
- html - 瞧,如何在 HTML 小部件中显示图像
- r - 在不关闭自动缩进的情况下粘贴到 RStudio 脚本窗格中而不自动格式化?
- java - 设置可信包 (ActiveMQ)
- python - 应用 lambda 函数在熊猫数据框中创建新列时出现内存错误
- azure-devops - 使用来自另一个项目(同一组织内)的工件提要作为上游源
- c# - Ajax 唤醒 Devexpress 组件
- android - 小部件库捕获的异常在构建 HomePageScreen(dirty, state: _HomePageScreenState#3ae53) 时引发了以下 ArgumentError:
- android - 在 Viewholder 中更改颜色语句不起作用;只有一个语句正常工作
- c# - 如何使用 C#.NET 将具有相同格式数据的多个 XML 文件合并到一个包含所有数据的 XML 文件中?