警告:imagettftext() [function.imagettftext]:无法在第 35 行的 /home/a


Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/a2424901/public_html/index.php on line 35

<?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上传到我网站的根目录。但我仍然显示错误 - Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/a2424901/public_html/index.php on line 35.我尝试更改案例,但我没有为我工作。我在这段代码中哪里出错了?

来自文档

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

这似乎意味着字体文件应该是一个绝对路径,如果不是,该函数将在其末尾附加另一个.ttf

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

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

或者省略.ttf并使用GDFONTPATH。 该文档建议如下:

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

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

添加到user2724960的答案;将字体名称更改为__DIR__ . '/graph/fonts/someFont.ttf'为我做到了。

全线:

$myPicture->setFontProperties(array("FontName"=>__DIR__ .  '/graph/fonts/someFont.ttf',"FontSize"=>14));

不要忘记将"someFont"替换为字体文件的名称(默认:"Forgotte")

我的解决方案(对我有用):

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

我也在 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 文件位于同一目录中)

我遇到了同样的问题。我的字体名称是

滴度.TTF

我把它改成了

滴度.ttf

它的工作非常完美。

它对我有用:使用绝对路径

$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);

如果您使用的是 pChart,请使用以下内容:

$myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));

在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';

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

我在处理本地主机(XAMPP)时遇到了同样的问题。

我的解决方案是:

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

请检查资源下的字体文件夹。