Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用GD在图像上写入文本无效_Php_Gd - Fatal编程技术网

Php 使用GD在图像上写入文本无效

Php 使用GD在图像上写入文本无效,php,gd,Php,Gd,我使用的是PHP7.4,当我尝试使用imagettftext()函数在图像上写入文本时,什么都不会发生,只有空白图像!当我回到PHP5.6时,它工作得非常好 我已经确认GD已启用,并且我在windows操作系统下 这是我的代码(我从中复制): “”是真的吗?我认为字体应该是这里的问题。 您应该将其包含在路径中。请阅读位于的文档 根据PHP使用的GD库的版本,当fontfile不以前导/then开头时,文件名将附加.ttf,库将尝试沿库定义的字体路径搜索该文件名 当使用低于2.0.18的GD库版本

我使用的是
PHP7.4
,当我尝试使用
imagettftext()
函数在图像上写入文本时,什么都不会发生,只有空白图像!当我回到
PHP5.6
时,它工作得非常好

我已经确认GD已启用,并且我在windows操作系统下

这是我的代码(我从中复制):


“”是真的吗?

我认为字体应该是这里的问题。 您应该将其包含在路径中。请阅读位于的文档

根据PHP使用的GD库的版本,当fontfile不以前导/then开头时,文件名将附加.ttf,库将尝试沿库定义的字体路径搜索该文件名

当使用低于2.0.18的GD库版本时,使用空格字符而不是分号作为不同字体文件的“路径分隔符”。无意中使用此功能将导致警告消息:警告:找不到/打开字体。对于这些受影响的版本,唯一的解决方案是将字体移动到不包含空格的路径


所以你应该确定字体在哪里。或者尝试使用前导“/”或不带文件扩展名的不同组合

您是否阅读了文档?“注意:只有在使用freetype支持编译PHP时,此函数才可用”。代码在w10下使用PHP7.4.2和$font=“C:\\Windows\\Fonts\\arial.ttf”;上面的代码对我来说也很好,路径正确chosen@emix是的,我读过那篇文章,但我使用共享主机,没有安装freetype的权限。@jspit我使用Windows 10作为操作系统,PHP版本7.4.11也可以!当我尝试包含所用字体的实际路径时。。。有效:)非常感谢@Alexander Klement
// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);