PHP找不到/打开字体。

3

我想在一张图片上写文字,但是我遇到了错误:PHP警告:imagettftext():无法在/var/www/html/anti-bot/image.php的第12行找到/打开字体。

这是我的代码:

<?php
session_start();
header("Content-Type: image/png");
$text = rand(1000,1000000);
$_SESSION['code']=$text;
$img= imagecreatefromjpeg("bg.jpg");
$font = 'arial.ttf';
$R = rand(0,100);
$G = rand(0,100);
$B = rand(0,100);
$TxtColor = imagecolorallocate($img,$R,$G,$B);
imagettftext($img,rand(40,45),rand(0,1),rand(10,70),rand(38,50),$TxtColor,$font,$text);
imagepng($img);
imagedestroy($img);

?>

我尝试过:

$font = 'var/www/html/anti-bot/arial.ttf";
$font = 'var/www/html/anti-bot/arial";
$font = 'arial.ttf";
$font = 'arial";

所有的文件都位于同一个目录下,即 var/www/html/anti-bot。

关于您的“I have tried”示例:前两个是无效的(引号损坏)。您是否尝试使用前导斜杠...即/var/www/..... - Simba
你的“代码”位于哪个文件夹/文件中? - TimBrownlaw
它与 /var/www/..... 一起工作了。谢谢,Simba。 - Dante
1个回答

0

你必须提供读取字体的绝对路径。

因此,请按以下方式提供字体路径:

$font = "/var/www/html/anti-bot/arial.ttf";

注意在var之前加上/,以从根目录获取路径


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接