imagettftext():无法找到/打开字体错误消息

5

我想要读取一个表单中的验证码图片。

以下是表单代码:

<form class="login" method="POST" action="">
    <p class="title">Are you human?</p>
    <div id="captchaimage">
        <img src="generate.php">
    </div>
    <input type="text" name="scr" size="6" placeholder="Write what you see here" autofocus/>
    <input class="submititon" type="submit" value="Submit" name="submit"></input>
</form>

这是generate.php文件:

<?php 
session_start();
//header('Content-type: image/jpeg');
$text = $_SESSION['scr'];
$font_size = 26;
$image_width = 100;
$image_height = 40;

$image = imagecreate($image_width, $image_height);
imagecolorallocate($image,255,255,255);
$text_color = imagecolorallocate($image,0,0,0);

for($x=1;$x<=30;$x++){
    $x1 = rand(1, 100);
    $y1 = rand(1, 100);
    $x2 = rand(1, 100);
    $y2 = rand(1, 100);

    imageline($image, $x1, $y1 ,$x2 ,$y2, $text_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>

如您所见,我已将header()注释掉以查看此页面返回的错误。错误如下: 警告: imagettftext(): 第25行找不到/打开字体 第25行:
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);

我不知道为什么会出现这个错误,因为font.ttf文件已经正确地放置在同一个目录下。
那么,这里到底发生了什么?!

2
最好使用__DIR__ . '/font.ttf'来指定文件的当前目录和绝对路径。这样你还可以打印字体路径并测试它是否真的存在,以防它不起作用。 - h2ooooooo
谢谢,现在它可以工作了。 - user9162601
3个回答

2

根据Maicon Perin的最初回答。

    $image = Image::canvas(750, 300, '#f3f3f3');
    $image->text($title, 375, 150, function($font) {
        $font->file(realpath('fonts/font-file.ttf'));
        $font->size(44);
        $font->color('#ffffff');
        $font->align('center');
        $font->valign('center');
    });

你只需要将字体文件添加到 | public/fonts/font-file.ttf

1

您需要提供字体文件的实际路径。请尝试以下方法:

realpath('path/to/you/font.ttf');

这种方法对我有效。

0

不要使用; 'font.ttf'

改为 => './font.ttf'


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