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

2022-08-30 18:02:26
<?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.ttfWarning: imagettftext() [function.imagettftext]: Could not find/open font in /home/a2424901/public_html/index.php on line 35


答案 1

从文档

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

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

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

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

或者省略 并使用 .本文档建议如下:.ttfGDFONTPATH

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

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

答案 2

添加到user2724960的答案;更改字体名称以为我完成此操作。__DIR__ . '/graph/fonts/someFont.ttf'

全线:

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

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


推荐