警告:imagettftext() [function.imagettftext]:在/home/a2424901/public_html/index.php的第35行找不到/打开字体。

24
<?php
session_start();
require_once 'facebook.php';
$app_id = "418907881455014";
$app_secret = "36389d2c4caaf6de86982cb87686a494";
$redirect_uri = 'http://gooogle12.comuf.com';
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');

$coded = $_REQUEST['code'];

$access_token = $facebook->getAccessToken();
$name = "".$user_profile['name']."";
$fbid = "".$user_profile['id']."";

function RandomLine($filename) {
    $lines = file($filename) ;
    return $lines[array_rand($lines)] ;
}
$reason = RandomLine("reason.txt");  

$canvas = imagecreatefromjpeg ("bg.jpg");                                   // background image file
$black = imagecolorallocate( $canvas, 0, 0, 0 );                         // The second colour - to be used for the text
$font = "Arial.ttf";                                                         // Path to the font you are going to use
$fontsize = 20;                                                             // font size

$birthday = "".$user_profile['birthday']."";
$death = "- ".date('d/m/Y', strtotime( '+'.rand(0, 10000).' days'))."";

imagettftext( $canvas, 22, -1, 110, 120, $black, $font, $name );            // name
imagettftext( $canvas, 22, -1, 110, 170, $black, $font, $birthday );        // birthday
imagettftext( $canvas, 22, -1, 255, 172, $black, $font, $death );           // death
imagettftext( $canvas, 20, -1, 110, 220, $black, $font, $reason );           // reason


$facebook->setFileUploadSupport(true);

//Create an album
$album_details = array(
        'message'=> 'How will you die?',
        'name'=> 'How will you die?'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);

//Get album ID of the album you've just created
$album_uid = $create_album['id'];

//Upload a photo to album of ID...

$file='img/'.$fbid.'.jpg'; //Example image file

$photo_details = array( 'message'=> 'Find...51', 'image' => '@'.realpath($file));
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);


    enter code here



ImageDestroy( $canvas );

header("Location: http://facebook.com".$fbid."&photoid=".$upphoto."")
?>

我正在使用这个php代码来制作一个Facebook应用程序。我将字体Arial.ttf上传到了我的网站根目录中。但是我仍然显示错误 - 警告:imagettftext() [function.imagettftext]:在/home/a2424901/public_html/index.php的第35行找不到/打开字体。我尝试更改大小写,但对我没有用。我在代码中哪里出错了?

10个回答

45

官方文档

根据 PHP 所使用的 GD 库版本不同,当 fontfile 没有以斜杠开头时,函数会在文件名后附加 .ttf 并尝试在预定义的字体路径下搜索该文件。

这似乎意味着 fontfile 应该是绝对路径,如果不是,则函数将在其末尾添加另一个 .ttf

请指定字体文件的完整路径。

$font = "/home/a2424901/public_html/Arial.ttf";

可以省略 .ttf,使用GDFONTPATH文档建议采用以下方法:

在许多情况下,字体与使用它的脚本位于同一目录中,以下提示将消除任何包含问题。

putenv('GDFONTPATH=' . realpath('.'));
$font = "Arial";

18
继续user2724960的回答; 将FontName更改为__DIR__ . '/graph/fonts/someFont.ttf'对我起了作用。
完整行:
$myPicture->setFontProperties(array("FontName"=>__DIR__ .  '/graph/fonts/someFont.ttf',"FontSize"=>14));

不要忘记用你的字体文件的名称(默认为“Forgotte”)替换“someFont”。

1
它可以工作了,谢谢。我原以为这是权限问题,但事实并非如此。 - arjunaaji

11

我的解决方法(适用于我):

realpath('here/is/right/path/to/font.ttf');

9

我也在使用XAMPP,显然XAMPP只支持完整的字体目录路径。这段代码在Windows(XAMPP)和我们提供的Linux服务器上都可以正常工作:

$dir= dirname(realpath(__FILE__));
$sep=DIRECTORY_SEPARATOR;   
$font =$dir.$sep.'arial.ttf';
imagettftext($thumb, $size, 0, $y_pos, $size, $textcolor, $font, $txt);

(假设您的字体文件与PHP文件在同一目录中)

1
我有同样的问题。我的字体名称是:

Titr.TTF

我把它改成了 <\p>。

Titr.ttf

并且它完美地运行了。

1
它对我有效:使用绝对路径。
$font = 'C:\wamp\www\Persian-txt2img\Vazir-Code.ttf';
$font = mb_convert_encoding($font, 'big5', 'utf-8');

// Add the text
imagettftext($image, 24, 0, 64, 48, $text_color, $font, $text);

0
如果您正在使用pChart,请使用以下内容:
$myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));

0

在本地主机(XAMPP)上工作时,我遇到了同样的问题。

我的解决方案是:

// Set Correct Path to Font File
$fontPath='C:\xampp\htdocs\Adeplay\fonts\Tesox\tesox.ttf'; 

0
在GoDaddy(共享主机,Cpanel)上,我必须在域名之后指定路径:
我的文件在这里:
URL:  http://example.com/assets/fonts/verdana/verdana.ttf
path:  /home/content/123456/html/example/assets/fonts/verdana/verdana.ttf

可运行的 PHP 代码:

$font = 'assets/fonts/verdana/verdana.ttf';

使用其他路径值不起作用。


-6
请在资源文件夹下检查字体文件夹。

3
这应该是一条评论,而不是答案。 - Tay2510

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